Created
January 11, 2018 22:19
-
-
Save leonardosnt/8390edfb187ce4a3146d70697ed49b8b to your computer and use it in GitHub Desktop.
Marshal container/list golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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