How do I initialize an array without using a for loop in Go?

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

How do I initialize an array without using a for loop in Go?

问题

我有一个布尔值数组 A,索引从整数 0n,初始值都设置为 true

我的当前实现是:

for i := 0; i < n; i++ {
    A[i] = true
}
英文:

I have an array A of boolean values, indexed by integers 0 to n, all initially set to true.

My current implementation is :

for i := 0; i &lt; n; i++ {
    A[i] = true
}

答案1

得分: 18

使用for循环是最简单的解决方案。创建一个数组或切片总是会返回一个零值。对于bool类型来说,这意味着所有的值都将是false(即bool类型的零值)。

请注意,使用复合字面量可以创建和初始化一个切片或数组,但这并不会更短:

b1 := []bool{true, true, true}
b2 := [3]bool{true, true, true}

如果你不想使用for循环,你可以通过引入一个常量来使代码变得更短,该常量表示true的值:

const T = true
b3 := []bool{T, T, T}

如果n很大,使用for循环是最简单的解决方案。

或者,你可以改变应用程序的逻辑,使用数组或切片来存储切片中的取反值,这样“全为false”的零值将成为一个很好的初始值。我的意思是,如果你的切片用于存储文件是否“存在”,你可以改变逻辑,使切片存储文件是否“缺失”:

presents := []bool{true, true, true, true, true, true}

// 等价于:

missings := make([]bool, 6) // 全为false
// missing=false 表示未缺失,表示存在

还要注意,用特定值填充数组或切片被称为“memset”操作。Go语言没有内置的函数来实现这个操作,但你可以参考这个问题中的高效解决方案:

https://stackoverflow.com/questions/30614165/is-there-analog-of-memset-in-go

英文:

Using a for loop is the simplest solution. Creating an array or slice will always return you a zeroed value. Which in case of bool means all values will be false (the zero value of type bool).

Note that using a Composite literal you can create and initialize a slice or array, but that won't be any shorter:

b1 := []bool{true, true, true}
b2 := [3]bool{true, true, true}

If you don't want to use a for loop, you can make it a little shorter by introducing a constant for the value true:

const T = true
b3 := []bool{T, T, T}

If n is big, for is the simplest solution.

Or you could switch the logic of your application, and use the array or slice to store the negated values in the slice, and that way the "all-false" zero value would be a good initial value. What I mean is that if your slice is to store if files are present, you could change the logic so the slice stores whether files are missing:

presents := []bool{true, true, true, true, true, true}

// Is equivalent to:

missings := make([]bool, 6) // All false
// missing=false means not missing, means present)

Also note that filling an array or slice with a specific value is known as a "memset" operation. Go does not have a builtin function for that, but for an efficient solution see this question:

https://stackoverflow.com/questions/30614165/is-there-analog-of-memset-in-go

答案2

得分: 0

使用range函数进行初始化,而不知道数组中元素的数量。

for i, _ := range A {
    A[i] = true
}

请注意,这段代码是使用Go语言编写的。它使用range函数遍历数组A的索引,并将每个元素设置为true。

英文:

Make initilization using range function without knowing the number of elements in the array.

for i,_:=range(A){A[i] = true}

huangapple
  • 本文由 发表于 2015年9月25日 23:11:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/32785319.html
匿名

发表评论

匿名网友

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

确定