golang 类型转换
1 import ( 2 "bytes" 3 "encoding/binary" 4 "encoding/gob" 5 "fmt" 6 ) 7 8 func str_to_byte(src string) []byte { 9 //strSlice := []string{src} 10 buffer := &bytes.Buffer{} 11 gob.NewEncoder(buffer).Encode(src) 12 return buffer.Bytes() 13 } 14 15 func uint64_to_byte(src uint64) []byte { 16 buf := make([]byte, 8) 17 binary.BigEndian.PutUint64(buf, src) 18 return buf 19 } 20 21 func main() { 22 var u uint64 23 u = 128 24 bytes := uint64_to_byte(u) 25 fmt.Println(bytes) 26 str := "128" 27 bytes = str_to_byte(str) 28 fmt.Println(bytes) 29 }