Golang conventions: Difference between revisions

From wikinotes
Line 36: Line 36:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Interfaces -->
</blockquote><!-- Interfaces -->
== Tests ==
<blockquote>
* Tests are kept alongside the code, and have the suffix <code>_test.go</code>
* Tests themselves are functions, with a prefix of <code>Test</code> followed by the name of the function being tested
* You may create subtests, evaluated by a top-level test, that can share setup/teardown code.
</blockquote><!-- Tests -->
</blockquote><!-- Naming -->
</blockquote><!-- Naming -->



Revision as of 21:56, 6 June 2022

Naming

Brevity

Go prefers short names, with good doc comments for both functions and packages.
Imported package names are typed every time a symbol is used.

Go also prefers you drop the get prefix of getters.

Casing

Functions

func DoThing() { ... }  // exported functions are PascalCase
func doThing() { ... }  // regular functions are camelCase

Variables

var URL string = "https://example.com"  // acronyms are uppercase
var catName string = "foo"              // regular vars are camelCase
var DogName string = "foo"              // constants in pascal-case are exported

Interfaces

// interfaces should end in 'er' (or similar)
Writer, Reader, Formatter, Notifier, Flusher, Stringer

// implementations of interfaces should drop the 'er'
Write, Read, Format, Notify, Flush, String

Tests

  • Tests are kept alongside the code, and have the suffix _test.go
  • Tests themselves are functions, with a prefix of Test followed by the name of the function being tested
  • You may create subtests, evaluated by a top-level test, that can share setup/teardown code.

Spacing

TODO

Syntax

Semicolons

Go uses semicolons as line-endings, but prefers that the semicolon is implied automatically.