I have a binary package of data, but length of the package is a first 4 bytes of it. Is there a correct way to read length and then read whole package with net.Conn in GoLang?
Try using this:
package main
import ( "encoding/binary" "io" )
func ReadPacket(r io.Reader) ([]byte, error) {
lenB := make([]byte, 4)
if _, err := r.Read(lenB); err != nil {
return nil, err
}
//you can use BigEndian depending on the proto
l := binary.LittleEndian.Uint32(lenB)
packet := make([]byte, l)
_, err := r.Read(packet)
return packet, err
}