string与int互转
#string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 64) #int到string string:=strconv.Itoa(int) #int64到string string:=strconv.FormatInt(int64,10)
int64与[]byte互转
package main import ( "fmt" "encoding/binary" ) func main() { var i int64 = 2323 buf := Int64ToBytes(i) fmt.Println(buf) fmt.Println(BytesToInt64(buf)) } func Int64ToBytes(i int64) []byte { var buf = make([]byte, 8) binary.BigEndian.PutUint64(buf, uint64(i)) return buf } func BytesToInt64(buf []byte) int64 { return int64(binary.BigEndian.Uint64(buf)) }
int之间互转
#高效写法 package main import ( "unsafe" ) func main() { // 设置一个 int64 的数据 int64_num := int64(6) // 将 int64 转化为 int int_num := *(*int)(unsafe.Pointer(&int64_num)) println(int_num) }
直接转换也可以
例如:
int(int64)
int64(int)
string与[]byte互转
[]byte 转 string:
string([]byte)
string 转 []byte:
[]byte(string)