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

I have an array Aof booleans indexed by integers 0before n, all are initially set to true.

My current implementation:

for i := 0; i < n; i++ {
    A[i] = true
}
+4
source share
2 answers

Using a loop foris the easiest solution. Creating an array or slice will always return a null value. Which in the case boolmeans that all values ​​will be false(zero type value bool).

, Composite literal , :

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

for, , true:

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

n , for - .

, "all-false" . , , , , :

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

// Is equivalent to:

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

, "memset". Go , . :

memset go?

+8

, , .

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

Source: https://habr.com/ru/post/1609013/


All Articles