thefuga / go-notes

My personal Go notes and resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Good practices

Formatting

  • Formatting should be performed by gofmt.

Comments

  • Packages should have a top block comment for documentation - multi file packages should only have one comment.

  • Comments are plain text, no annotations for formatting is needed.

  • Comments preceding top-level declarations are used for documentation. Every exported name should have a doc comment.

    // Foo is an example of how doc comments should be.
    func Foo() {
    	return "foo"
    }
  • Related constants or variables may have only one comment to introduce them.

    // Dimentions of something.
    var(
    	length uint32
    	width uint32
    )

Names

Names have a semantic effect in Go. So naming is very important.

  • Go uses cammelCase and PascalCase to multiword names.
  • Names preceded with upper case are exported.

Packages

  • Package names should refer to it's content. They should be short, concise and evocative. Package names MUST:
    • Have one word.
    • Be lower case.
    • NOT have mixedCase or under_scores.

Getters and setters

  • Getters and setters may be used when necessary.
  • Getters should not take the prefix get . If there's a field name it's getter must be called Name .
  • Setters should take the prefix set. If there's a field name it's setter must be called SetName .

Interfaces

  • One-method interfaces should be named with the method's name followed by the suffix er . E.g. Reader , Writer .
  • Well-known method's names such as Read , Write , Flush, Close, should only be used if they have the same signature and meaning. E.g. a string converter should be called String not ToString.

Arrays and Slices

  • Array pointers like array *[10]float64 are not idiomatic and should be avoided. Slices should be used instead.

Style guides

Links

Reading

Links

Videos

Talks

About

My personal Go notes and resources