GoLang Naming Rules and Conventions

Kassim Damilola
2 min readNov 8, 2019

--

The Go project stress a lot on simplicity and readability, hence it adopts some simple convention to ensure that source codes are maintainable.

You must have already seen some of this in the Go Tutorial and Effective Go. This article lists only itemize some well known conventions in writing your Go project.

Files

  1. Go follows a convention where source files are all lower case with underscore separating multiple words.
  2. Compound file names are separated with _
  3. File names that begin with “.” or “_” are ignored by the go tool
  4. Files with the suffix _test.go are only compiled and run by the go test tool.

Functions and Methods

Use camel case, exported functions should start with uppercase:

writeToDB // unexported, only visible within the packageWriteToDB // exported, visible within the package

Constants

Constant should use all capital letters and use underscore _ to separate words.

Variables

  1. Generally, use relatively simple (short) name.
  2. Consistent naming style should be used the entire source code

user to u

userID to uid

  • If variable type is bool, its name should start with Has, Is, Can or Allow, etc.
  • Single letter represents index: i, j, k

Dave has an excellent article on wriing good and quality Go

--

--