arrays - Golang - 如何将数组 ( [ ] ) "convert"转换为列表 ( ... )?

标签 arrays go

我正在使用 UI 库 ( https://github.com/andlabs/ui ) 制作一个关于学生团体的程序。

ui.SimpleGrid 允许输入控件“列表”:

func NewSimpleGrid(nPerRow int, controls ...Control) SimpleGrid

我觉得在 Java 和其他语言中,它就像一个数组一样工作,这基本上意味着给它一个就可以了。然而,这在 Go 中似乎并不相同。

func initStudentsGrid(students ...Student) ui.SimpleGrid {
var i int
var grd_studentsList []ui.Grid

for i = 0; i < len(students); i++ {
    grd_student := ui.NewGrid()

    grd_student.Add(ui.NewLabel(students[i].group), nil, ui.West, true, ui.LeftTop, true, ui.LeftTop, 1, 1)
    grd_student.Add(ui.NewLabel(students[i].lastName), nil, ui.West, true, ui.LeftTop, true, ui.LeftTop, 1, 1)
    grd_student.Add(ui.NewLabel(students[i].firstName), nil, ui.West, true, ui.LeftTop, true, ui.LeftTop, 1, 1)

    grd_studentsList = append(grd_studentsList, grd_student)
}

return ui.NewSimpleGrid(1, grd_studentsList)

程序没有编译因为:

cannot use grd_studentsList (type []ui.Grid) as type ui.Control in argument to ui.NewSimpleGrid: []ui.Grid does not implement ui.Control (missing ui.containerHide method)

有什么方法可以将数组“转换”为所需的格式,因为不可能一个一个地添加网格(SimpleGrid 上没有附加方法)?

最佳答案

尝试这样的事情:

return ui.NewSimpleGrid(1, grd_studentsList...)
                                           ^^^^

规范中提到了这一点 Passing arguments to ... parameters .

关于arrays - Golang - 如何将数组 ( [ ] ) "convert"转换为列表 ( ... )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29031625/

相关文章:

arrays - 从数组创建字典但键唯一性有问题

php - 是否有将PHP函数与Go版本进行比较的引用?

algorithm - 用于建模包含/复合关系的数据结构

c# - 如何将数组传递给采用 RedisValue[] 的 StackExchange.Redis 方法?

python - 在 NumPy 中将字典转换为数组

c - 非常奇怪的数组声明问题 - C

javascript - 带有 Json 对象的 React-stockcharts

go - 如何配置GOPATH?

go - 表上的 OnChange 在 golang 驱动程序中可用 - rethinkDB?

user-interface - 使用 tivo/tview GoLang 库时获取实际的表单值