英文:
Create array of array literal in Golang
问题
如何在Golang中使用切片字面量创建一个int数组的数组?
我尝试了以下两种方式:
test := [][]int{{1,2,3},{1,2,3}}
和
type Test struct {
foo [][]int
}
bar := Test{foo: [][]int{{1,2,3}, {1,2,3}}}
这样就可以创建一个int数组的数组了。
英文:
How do I create an array of int arrays in Golang using slice literals?
I've tried
test := [][]int{[1,2,3],[1,2,3]}
and
type Test struct {
foo [][]int
}
bar := Test{foo: [[1,2,3], [1,2,3]]}
答案1
得分: 50
你几乎做对了,但是你内部数组的语法有点问题,需要使用花括号,像这样:test := [][]int{[]int{1,2,3},[]int{1,2,3}}
或者稍微简洁一些的写法:test := [][]int{{1,2,3},{1,2,3}}
这个表达式被称为“复合字面量”,你可以在这里了解更多信息:https://golang.org/ref/spec#Composite_literals
但是作为一个基本的经验法则,如果你有嵌套的结构,你必须递归地使用这种语法。它非常冗长。
英文:
You almost have the right thing however your syntax for the inner arrays is slightly off, needing curly braces like; test := [][]int{[]int{1,2,3},[]int{1,2,3}}
or a slightly more concise version; test := [][]int{{1,2,3},{1,2,3}}
The expression is called a 'composite literal' and you can read more about them here; https://golang.org/ref/spec#Composite_literals
But as a basic rule of thumb, if you have nested structures, you have to use the syntax recursively. It's very verbose.
答案2
得分: 6
在一些其他的语言(Perl、Python、JavaScript)中,[1,2,3]
可能是一个数组字面量,但在 Go 语言中,复合字面量 使用花括号,并且在这里,你必须指定外部切片的类型:
package main
import "fmt"
type T struct{ foo [][]int }
func main() {
a := [][]int{{1, 2, 3}, {4, 5, 6}}
b := T{foo: [][]int{{1, 2, 3}, {4, 5, 6}}}
fmt.Println(a, b)
}
你可以在 Playground 上运行或测试它。
Go 编译器足够聪明,可以推断出 [][]int
的元素是 []int
,无需在每个元素上都声明类型。但是,你必须写出外部类型的名称。
英文:
In some other langauges (Perl, Python, JavaScript), [1,2,3]
might be an array literal, but in Go, composite literals use braces, and here, you have to specify the type of the outer slice:
package main
import "fmt"
type T struct{ foo [][]int }
func main() {
a := [][]int{{1, 2, 3}, {4, 5, 6}}
b := T{foo: [][]int{{1, 2, 3}, {4, 5, 6}}}
fmt.Println(a, b)
}
You can run or play with that on the Playground.
The Go compiler is just tricky enough to figure out that the elements of an [][]int
are []int
without you saying so on each element. You do have to write out the outer type's name, though.
答案3
得分: 6
只需将方括号替换为花括号即可。在Go语言中,数组字面量使用花括号进行标识。
test := [][]int{{1,2,3},{1,2,3}}
英文:
Just replace the square brackets with curly braces. In Go, array literals are identified with curly braces.
test := [][]int{{1,2,3},{1,2,3}}
答案4
得分: 1
一个切片字面量的写法是[]type{<value 1>, <value 2>, ... }
。一个整数切片可以写作[]int{1,2,3}
,一个整数切片的切片可以写作[][]int{[]int{1,2,3},[]int{4,5,6}}
。
groups := [][]int{[]int{1,2,3},[]int{4,5,6}}
for _, group := range groups {
sum := 0
for _, num := range group {
sum += num
}
fmt.Printf("数组 %+v 的和为 %d\n", group, sum)
}
英文:
A slice literal is written as []type{<value 1>, <value 2>, ... }
. A slice of ints would be []int{1,2,3}
and a slice of int slices would be [][]int{[]int{1,2,3},[]int{4,5,6}}
.
groups := [][]int{[]int{1,2,3},[]int{4,5,6}}
for _, group := range groups {
sum := 0
for _, num := range group {
sum += num
}
fmt.Printf("The array %+v has a sum of %d\n", sub, sum)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论