In C++ I can initialize an array with some value using memset:
const int MAX = 1000000;
int is_prime[MAX]
memset(is_prime, 1, sizeof(is_prime))
What memset does, crudely can be described as filling the array with some value, but doing this really really fast.
In go I can do is_prime := make([]int, 1000000)
, but this will create a slice with all 0, in the similar manner I can use new([1000000]int)
, but nothing will allow me to create an array/slice with all 1 or any other non-zero element.
Of course I can use a loop to populate it with the value later, but the main purpose of memset
is that it is way way faster than the loop.
So do Go programmers have a memset
analog (fast way of initializing array to some non-zero value)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…