package iorw import ( "encoding/binary" "io" ) func WriteStr(w io.Writer, message string) error { messageBytes := []byte(message) sizeBytes := make([]byte, 8) binary.BigEndian.PutUint64(sizeBytes, uint64(len(messageBytes))) if _, err := w.Write(sizeBytes); err != nil { return err } if _, err := w.Write(messageBytes); err != nil { return err } return nil } func ReadStr(r io.Reader) (string, error) { sizeBytes := make([]byte, 8) if _, err := io.ReadFull(r, sizeBytes); err != nil { return "", err } messageSize := binary.BigEndian.Uint64(sizeBytes) messageBytes := make([]byte, messageSize) if _, err := io.ReadFull(r, messageBytes); err != nil { return "", err } return string(messageBytes), nil }