io.go (465B)
1 package util 2 3 import ( 4 "bytes" 5 "io" 6 ) 7 8 func ReadString(r io.Reader) (string, error) { 9 b := make([]byte, 1) 10 buf := bytes.NewBuffer(nil) 11 for { 12 if _, err := r.Read(b); err != nil { 13 return "", err 14 } 15 if b[0] == 0 { 16 return buf.String(), nil 17 } 18 buf.Write(b) 19 } 20 } 21 22 func WriteString(w io.Writer, s string) error { 23 if _, err := w.Write([]byte(s)); err != nil { 24 return err 25 } 26 if _, err := w.Write([]byte{0}); err != nil { 27 return err 28 } 29 return nil 30 }