I am looking to convert a string array to a byte array in GO so I can write it down to a disk. What is an optimal solution to encode and decode a string array ([]string
I would suggest to use PutUvarint and Uvarint for storing/retrieving len(s)
and using []byte(str)
to pass str
to some io.Writer
. With a string length known from Uvarint
, one can buf := make([]byte, n)
and pass the buf
to some io.Reader
.
Prepend the whole thing with length of the string array and repeat the above for all of its items. Reading the whole thing back is again reading first the outer length and repeating n-times the item read.