Skip to content

Instantly share code, notes, and snippets.

@leonardosnt
Created January 11, 2018 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardosnt/8390edfb187ce4a3146d70697ed49b8b to your computer and use it in GitHub Desktop.
Save leonardosnt/8390edfb187ce4a3146d70697ed49b8b to your computer and use it in GitHub Desktop.
Marshal container/list golang
func MarshalList(list *list.List) ([]byte, error) {
buffer := bytes.NewBufferString("[")
for el := list.Front(); el != nil; el = el.Next() {
marshalled, err := json.Marshal(el.Value)
if err != nil {
return nil, err
}
buffer.WriteString(string(marshalled))
if el.Next() != nil { // Has more elements, lets append a comma
buffer.WriteRune(',')
}
}
buffer.WriteString("]")
return buffer.Bytes(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment