Apr 19, 2020

[Go] Best practice trivial update


  • Check defer call error.
    e.g
    https://github.com/thanos-io/thanos/blob/master/pkg/runutil/runutil.go#L138
  • For graceful shutdown, exhaust reader before exit.
    e.g
    https://github.com/thanos-io/thanos/blob/master/pkg/runutil/runutil.go#L148
  • no Init(), no extern globals (only const allowed)
  • Don't use panic (I do use panic for writing to closed channel for graceful shutdown)
  • Always measure the results.
    https://vsdmars.blogspot.com/2016/01/c.html
  • dereference pointer and write to it is slow (this is basic if coming from C++/C) due to it's really an read and write (not single write)
  • Pre-allocating Slices and Maps (same if coming from C++: STL, std::vector is really a new allocate and copy)
  • Reuse underline allocated memory from slice:
    slice = slice[:0]
    Slice data structure same as SOO(small object optimization) in std::string,
    pointer to slice, length of current slice, slice mallocaed size / data type (i.e capacity).
  • blank _ tips: use for assuring a type implements an interface:
     var _ InterfaceA = TypeA

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.