Using binary.Write/ binary.Read:
err := binary.Write(connection, binary.LittleEndian, fileInfo.Size())
if err != nil {
fmt.Println("err:", err)
}
var size int64
err := binary.Read(connection, binary.LittleEndian, &size)
if err != nil {
fmt.Println("err:", err)
}
[]byte(string(fSize)) doesn’t do what you think it does, it treats the number as a Unicode character, it does not return a string representation.
If you need a string representation of a number, use strconv.Itoaif you want the binary representation to use:
num := make([]byte, 8) // or 4 for int32 or 2 for int16
binary.LittleEndian.PutUint64(num, 1<<64-1)
source
share