Go lang Naming Conventions

santosh
2 min readAug 31, 2022

Recently started coding in go-lang. Have compiled guidelines around various naming convention for file name, folder, interface, package, variable.

Hope this help others in quick-start

File Name

Go follows a convention where source files are all lower case with underscore separating multi words,

  • Example fmt.go, fmt_test.go, gostringer_example_test.go

Folder Name

Folder name should be all lower case with. “-” as separator for multiword

github.com/modern-go/concurrent

Variable Name

Go prefer short, concise, evocative

  • local variables should be single letter like i for index, e for employee
  • local variable for collections like slice, array, map should be double single letter word like pp for slice of person
dd:= [...]int{1,2,3}
for _, i := range dd {
fmt.Println(i)
}
  • Structure Name

The convention in Go is to use MixedCaps or mixedCaps (simply camelCase) rather than underscores to write multi-word names. If an identifier needs to be visible outside the package, its first character should be uppercase. If you don’t have the intention to use it in another package, you can safely stick to mixedCaps

package io

type eofReader struct{}
type multiReader struct {
readers []Reader
}
// A PipeReader is the read half of a pipe.
type PipeReader struct {
p *pipe
}

Getters, Setters

It is not necessary to put Get into the getter's name. If you have a field called owner (lower case, unexported), the getter method should be called Owner (upper case, exported), not GetOwner.

The setter function, if needed, will likely be called SetOwner. Both names read well in practice:

owner := obj.Owner() // getter
if owner != user {
obj.SetOwner(user)
}

Interface Names

By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: Reader, Writer, Formatter, CloseNotifier etc.

type Stringer interface {
String() string
}

Package

Packages are given lower case, single-word names; there should be no need for underscores or mixedCaps.

In case of multiword we keep lowercase only

package bytespackage ioutilpackage httptest

--

--