SoFunction
Updated on 2025-03-02

Detailed explanation of the naming specifications recommended by Golang

Golang recommended naming specifications

It is rare for people to summarize some naming specifications, which may be because the author is ignorant. As a two-year golang developer, I have summarized some common naming specifications based on many well-known projects, such as moby, kubernetess, etc. Naming specifications can make the code easier to read and fewer errors.

If you have any different opinions, please feel free to complain or discuss.Project gallery

File naming specifications

Since the file has nothing to do with the package and avoids the problem of Windows case, the recommended specifications are as follows: File names should be used in lowercase, and different words should be divided by underscores. Naming should be as good as possible.

Constant naming specification

The example of the name of the constant is clearly named by camelcase is as follows

const todayNews = "Hello"

// If a constant is exceeded, it should be organized in brackets
const (
  systemName = "What"
  sysVal = "dasdsada"
)

Variable naming specification

Like constant naming methods, variables should also use camel naming methods, but be careful not to be consistent with or start with the package name.

var x string
x := new(string)

Function naming specification

Due to Golang's special nature (using case to control the visibility of functions), except for special performance tests and unit test functions, the following principles should be followed.

  1. Naming with camel
  2. If you do not need to access outside the package, please start with lowercase functions
  3. If you need to expose it to outside the package, you need to use the function name starting with capitalization

A typical function naming method is as follows:

// All comments are always made using double slashes, the method of object exposurefunc (*fileDao) AddFile(file *) bool {
  result := (*file)
  if result {
   (file)
  }
  return result
}
 
// The functions that do not need to be accessed outside the package are as followsfunc removeCommaAndQuote(content string) string {
  re, _ := ("[\\`\\,]+")
  return ((content, ""))
}

Interface naming specifications

Interface naming also follows the camel method. You can use type alias to define type starting with capitalization for out-of-package access.

type helloWorld interface {
  func Hello();
}

type SayHello helloWorld

Struct Naming Specification

Similar to interface naming specification

receiver naming specification

There is a concept of receiver in golang. The receiver name should be consistent as much as possible. Avoid some semantic keywords in other languages ​​such as this, super, and other languages ​​as follows.

type A struct{}

func (a *A) methodA() {
}
func (a *A) methodB() {
  ()
}

Comment specifications

Double slashes should be used in comments

other

Format, use tab instead of space, can be compatible with go fmt

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.