Golang conventions: Difference between revisions

From wikinotes
Line 11: Line 11:
== Casing ==
== Casing ==
<blockquote>
<blockquote>
=== Functions ===
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
func DoThing() { ... }  // exported functions are PascalCase
func DoThing() { ... }  // exported functions are PascalCase
func doThing() { ... }  // regular functions are camelCase
func doThing() { ... }  // regular functions are camelCase
</syntaxhighlight>
=== Variables ===
<syntaxhighlight lang="go">
var URL string = "https://example.com"  // acronyms are uppercase
var catName = "foo"                    // regular vars are camelCase


</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:14, 29 May 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 = "foo"                     // regular vars are camelCase

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

Spacing

TODO

Syntax

Semicolons

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