The way Convert an integer to a byte array with golang
Use the encoding/binary
library
package main
import (
"encoding/binary"
"fmt"
)
func main() {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, 31415926)
fmt.Println(bs)
}
use int -> string -> byte
bs := []byte(strconv.Itoa(31415926))
binary.Write(a, binary.LittleEndian, myInt)
binary.LittleEndian.PutUint32(bs, value)
binary.BigEndian.PutUint32(b, i)
0
See Also
- Golang mod import local package
- Golang upnp/ssdp DiscoverDevices example
- Full LRU/FIFO Cache Implementation in Golang
- Golang Generics For Non-Beginners
- Get CPU Information with Pure Golang Code