Golang – 如何将数组([ ])转换为列表(…)?

huangapple go评论95阅读模式
英文:

Golang - How to "convert" an array ( [ ] ) to a list ( ... )?

问题

我正在使用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)

}

程序无法编译,因为:

> 无法将grd_studentsList(类型为[]ui.Grid)作为ui.NewSimpleGrid的参数类型ui.Control使用:[]ui.Grid没有实现ui.Control(缺少ui.containerHide方法)

是否有办法将数组转换为所需的格式,因为无法逐个添加网格(SimpleGrid上没有append方法)?

英文:

I'm using the UI lib (https://github.com/andlabs/ui) to make a program about students' groups.

The ui.SimpleGrid allows a "list" of Control to be entered:

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

I feel like in Java and other languages, it worked just as an array, which basically means that giving it one would work. However, this appears not to be the same in Go.

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

for i = 0; i &lt; 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)

The program's not compiling because:

> 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)

Is there any way to do a kind of "cast" from an array to the required format, since it isn't possible to add the grids one by one (no append method on SimpleGrid)?

答案1

得分: 10

尝试这样写:

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

这在规范中有提到将参数传递给...参数

英文:

Try something like:

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

This is mentioned in the spec Passing arguments to ... parameters.

答案2

得分: 4

> 它还是不起作用...

无法将 grd_studentsList(类型为 `[]ui.Grid`)作为类型 `[]ui.Control` 的参数传递给 `ui.NewSimpleGrid`

如我之前提到的(“关于内存布局,为什么在Go中无法将[]T转换为[]interface{}”),你不能隐式地将 A[] 转换为 []B

你需要先进行复制:

var controls []ui.Control = make([]ui.Control, len(grd_studentsList))
for i, gs := range grd_studentsList{
    controls [i] = gs
}

然后使用正确的切片(具有正确的类型)

 return ui.NewSimpleGrid(1, controls...)
英文:

> It it doesn't work either...

cannot use grd_studentsList (type `[]ui.Grid`) as type `[]ui.Control`
in argument to `ui.NewSimpleGrid`

As I mentioned before ("What about memory layout means that []T cannot be converted to []interface{} in Go?"), you cannot convert implicitly A[] in []B.

You would need to copy first:

var controls []ui.Control = make([]ui.Control, len(grd_studentsList))
for i, gs := range grd_studentsList{
    controls [i] = gs
}

And then use the right slice (with the right type)

 return ui.NewSimpleGrid(1, controls...)

huangapple
  • 本文由 发表于 2015年3月13日 19:56:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/29031625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定