DEV Community

loading...

Discussion on: Attempting to Learn Go - Building Dev Log Part 03

Collapse
krusenas profile image
Karolis

Hi, good job :)

Try avoiding things like f *[]byte as slices and maps are already pointers by themselves. Also, it's a good practice to check whether entries exist in the map before converting them to types:

p.Published = fm["published"].(bool)

Could be:

pubIntf, ok := fm["published"]
if ok {
   published, ok := pubIntf.(bool)
   if ok {
     p.Published = published
   }
}

While it does seem redundant in personal/toy projects, I have seen some applications panic and crash due to such simple assumptions when someone later modifies the input map and misses some values :)

P.S. It would be a lot more robust if you parsed that yaml into a struct!

Collapse
shindakun profile image
Steve Layton Author

Thanks for the comment! I've been putting off dealing with checking on the front matter map for two posts. 😅 isNil() basically only exists for checking the map but I keep putting off implementing the full thing because I was working with "known good" files. But I kind of like the pattern in your example I will probably be using that. I think at some point it might go into a struct if I keep extending the code.