Go Proper Naming Convention

Naming in Go is one of the most confusing parts for me, as someone who has come from a background in JavaScript. So here I'm sharing some pointers that would help newcomers who are just getting started with Go.

  • Exported name should be an uppercase character.

  • When a variable, struct or interface is imported from another package, it's name includes a package name or alias, for example, sys.GetId.

  • Packages names are generally lowercase, also the reference includes the package name -- so you should not prefix that name with any variable or function. For eg: the package xml should have a function with the name Reader, not XMLReader.

  • Variable names should be short rather than long.

  • The first sentence of the comment should start with the name being exported and provide a summary of it.

  • Avoid name collisions, when possible. For example, if you introduce a set of string functions, avoid calling it strings package because a package with the same name exists in the Go standard library and is already widely used.

  • Single-method interfaces are named using the method name plus an er suffix. For example, an interface with a Write function would be called Writer.

These are some of the naming convention that I found helpful.

Following points are taken from the book Microservices with Go, you can definitely check it out to increase your knowledge base about go.