in golang, having a []byte
(resulting from ioutil.ReadAll
), i should need to read from the first newline CRLF until the end of the block.
could you point me some examples?
in golang, having a []byte
(resulting from ioutil.ReadAll
), i should need to read from the first newline CRLF until the end of the block.
could you point me some examples?
This is can be done easily using the bytes.Index
function, which returns the index of a given sub-slice of bytes in another byte slice:
func afterCRLF(data []byte) []byte {
crlf := []byte("
")
index := bytes.Index(data, crlf)
if index == -1 {
return nil
}
return data[index+len(crlf):]
}
Note: the above function does not include the first CRLF in the returned byte slice. Remove +len(crlf)
if you need those two characters included.
Example: https://play.golang.org/p/WdylrkPwU_