初始字节数组有没有简便的方法?将字符串转换为固定大小的字节数组中的字段
package main
import "fmt"
type T1 struct {
f1 [5]byte // I use fixed size here for file format or network packet format.
f2 int32
}
func main() {
t := T1{"abcde", 3}
// t:= T1{[5]byte{'a','b','c','d','e'}, 3} // work, but ugly
fmt.Println(t)
}
prog.go:8:不能使用 “ABCDE”(类型字符串)作为类型[5] UINT8在字段值
如果我行更改为t := T1{[5]byte("abcde"), 3}
prog.go: 8:不能转换“abcde”(类型字符串)键入[5] uint8
这类似于:http://stackoverflow.com/questions/8032170/how-to-assign-string-to-bytes-array。 – jimt