在Golang中初始化接口数组 [英] Initialize array of interfaces in Golang

查看:40
本文介绍了在Golang中初始化接口数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是变量的示例:

名称:= []界面{} {第一",第二"}

如何从字符串数组中动态初始化它?

How can it be initialized dynamically, from an array of strings?

推荐答案

strs := []string{"first", "second"}
names := make([]interface{}, len(strs))
for i, s := range strs {
    names[i] = s
}

会是最简单的

这篇关于在Golang中初始化接口数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆