I want to set an array of bytes. I'm writing this:
var FooArr [size]byte = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Goland tells me "expression or string literal expected", what am i doing wrong?
Thanks!
评论:
klauspost:
8lall0:You can either do this:
var foo [16]byte = [...]byte { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
or this (nicer)
var foo = [...]byte { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
nevyn:Thanks!
nevyn:Note that the second version doesn't tell the programmer (reader) what the size of the array is. In this case you can solve that by having 4 groups of 4 values, but in general it can be better to add the extra "[16]byte" even if you think it looks uglier.
kl0nos:Note that zero values are special in golang, so both:
var foo1 [16]byte = [16]byte{0} var foo2 [16]byte
...will also work.
FooArr := []byte{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
