import ( "bufio" "io" ) func processChunk(data []byte) { // Process data } func readBinaryFile(reader io.Reader) error { scanner := bufio.NewScanner(reader) scanner.Split(bufio.ScanBytes) buf := make([]byte, 1024) for scanner.Scan() { data := scanner.Bytes() if len(data) != 0 { copy(buf, data) processChunk(buf) } } if scanner.Err() != nil { return scanner.Err() } return nil }
import ( "bufio" "encoding/binary" "io" ) func readMessage(reader io.Reader) error { scanner := bufio.NewScanner(reader) scanner.Split(bufio.ScanBytes) // Read message length var length uint32 for i := 0; i < 4; i++ { if !scanner.Scan() { return scanner.Err() } byte := scanner.Bytes()[0] length += uint32(byte) << uint32((3-i)*8) } // Read message data data := make([]byte, length) for i := 0; i < int(length); i++ { if !scanner.Scan() { return scanner.Err() } data[i] = scanner.Bytes()[0] } // Process message processMessage(data) if scanner.Err() != nil { return scanner.Err() } return nil }Package library: bufio, encoding/binary