I try to write a small application in go that takes 'x' numbers of integers from standard input, calculates the mean and gives it back. I have only gotten so far:
我试着写一个小的应用程序,它从标准输入中取“x”的整数,计算平均值,然后返回。到目前为止,我只做到了:
func main() {
var elems, mean int
sum := 0
fmt.Print("Number of elements? ")
fmt.Scan(&elems)
var array = new([elems]int)
for i := 0; i < elems; i++ {
fmt.Printf("%d . Number? ", i+1)
fmt.Scan(&array[i])
sum += array[i];
}............
When trying to compile this I get the following error message:
在尝试编译时,我得到以下错误消息:
invalid array bound elems
无效的数组绑定elem
What is wrong here?
什么是错误的吗?
3 个解决方案
#1
57
you should use a slice:
你应该使用切片:
//var array = new([elems]int) - no, arrays are not dynamic
var slice = make([]int,elems) // or slice := make([]int, elems)
see "go slices usage and internals"
参见“go slice用法和内部细节”
Also you may want to consider using range for your loop:
你也可以考虑使用范围for your loop:
// for i := 0; i < elems; i++ { - correct but less idiomatic
for i, v := range slice {
#2
8
In my opinion, this results from confusion over the usage of the new
and make
functions. This is a known issue/feature in the Go language, as evidenced by several discussions about new
vs make
at golang-nuts.
在我看来,这是由于对新函数的使用和make函数的混淆造成的。在Go语言中,这是一个众所周知的问题/特性,在golang-nuts会议上关于新vs make的讨论就证明了这一点。
The difference between new
and make
may become clearer by letting Go print out the type of the value created by new
and make
:
new和make之间的区别可以通过打印new创建的值的类型而变得更加清晰:
package main
import "fmt"
func main() {
fmt.Printf("%T %v\n", new([10]int), new([10]int))
fmt.Printf("%T %v\n", make([]int, 10), make([]int, 10))
}
The output:
输出:
*[10]int &[0 0 0 0 0 0 0 0 0 0]
[]int [0 0 0 0 0 0 0 0 0 0]
As can be seen from the type, to access an array element of new([10]int)
we would first need to dereference the pointer.
从类型中可以看出,要访问new([10]int)的数组元素,我们首先需要取消对指针的引用。
Both new
and make
require a Go type as their 1st argument. However, the expression [elems]int
is not a Go type (unless elems
is a Go constant, which isn't the case here).
new和make都需要Go类型作为第一个参数。但是,表达式[elems]int不是Go类型(除非elems是Go常量,这里不是这种情况)。
For further reference, see http://golang.org/doc/go_spec.html#Allocation and http://golang.org/doc/go_spec.html#The_zero_value.
要进一步参考,请参见http://golang.org/doc/go_spec.html#分配和http://golang.org/doc/go_spec.html#The_zero_value。
To get a better understanding of whether the result of new
is usable, it may be helpful to lookup whether len
and cap
work with zero (nil) values: http://golang.org/doc/go_spec.html#Length_and_capacity
要更好地了解new的结果是否可用,可以查找len和cap是否使用0 (nil)值:http://golang.org/doc/go_spec.html#Length_and_capacity
#3
1
See The Go Programming Language Specification
参见Go编程语言规范
http://golang.org/ref/spec#Array_types
http://golang.org/ref/spec Array_types
http://golang.org/ref/spec#Constants
http://golang.org/ref/spec常量
It says:"The length is part of the array's type; it must evaluate to a non- negative constant representable by a value of type int. "
它说:“长度是数组类型的一部分;它的值必须是非负常数,可以用int类型的值表示。”
Constants by no means vary.
常数绝不是变化的。
#1
57
you should use a slice:
你应该使用切片:
//var array = new([elems]int) - no, arrays are not dynamic
var slice = make([]int,elems) // or slice := make([]int, elems)
see "go slices usage and internals"
参见“go slice用法和内部细节”
Also you may want to consider using range for your loop:
你也可以考虑使用范围for your loop:
// for i := 0; i < elems; i++ { - correct but less idiomatic
for i, v := range slice {
#2
8
In my opinion, this results from confusion over the usage of the new
and make
functions. This is a known issue/feature in the Go language, as evidenced by several discussions about new
vs make
at golang-nuts.
在我看来,这是由于对新函数的使用和make函数的混淆造成的。在Go语言中,这是一个众所周知的问题/特性,在golang-nuts会议上关于新vs make的讨论就证明了这一点。
The difference between new
and make
may become clearer by letting Go print out the type of the value created by new
and make
:
new和make之间的区别可以通过打印new创建的值的类型而变得更加清晰:
package main
import "fmt"
func main() {
fmt.Printf("%T %v\n", new([10]int), new([10]int))
fmt.Printf("%T %v\n", make([]int, 10), make([]int, 10))
}
The output:
输出:
*[10]int &[0 0 0 0 0 0 0 0 0 0]
[]int [0 0 0 0 0 0 0 0 0 0]
As can be seen from the type, to access an array element of new([10]int)
we would first need to dereference the pointer.
从类型中可以看出,要访问new([10]int)的数组元素,我们首先需要取消对指针的引用。
Both new
and make
require a Go type as their 1st argument. However, the expression [elems]int
is not a Go type (unless elems
is a Go constant, which isn't the case here).
new和make都需要Go类型作为第一个参数。但是,表达式[elems]int不是Go类型(除非elems是Go常量,这里不是这种情况)。
For further reference, see http://golang.org/doc/go_spec.html#Allocation and http://golang.org/doc/go_spec.html#The_zero_value.
要进一步参考,请参见http://golang.org/doc/go_spec.html#分配和http://golang.org/doc/go_spec.html#The_zero_value。
To get a better understanding of whether the result of new
is usable, it may be helpful to lookup whether len
and cap
work with zero (nil) values: http://golang.org/doc/go_spec.html#Length_and_capacity
要更好地了解new的结果是否可用,可以查找len和cap是否使用0 (nil)值:http://golang.org/doc/go_spec.html#Length_and_capacity
#3
1
See The Go Programming Language Specification
参见Go编程语言规范
http://golang.org/ref/spec#Array_types
http://golang.org/ref/spec Array_types
http://golang.org/ref/spec#Constants
http://golang.org/ref/spec常量
It says:"The length is part of the array's type; it must evaluate to a non- negative constant representable by a value of type int. "
它说:“长度是数组类型的一部分;它的值必须是非负常数,可以用int类型的值表示。”
Constants by no means vary.
常数绝不是变化的。