SoFunction
Updated on 2025-03-05

Golang's naming specifications and best practices (recommended!)

Preface

Golang is a case-sensitive language.

Naming rulesInvolves: variables, constants, global functions, structures, interfaces, methods.

1. Capsule specifications

  • Any name that needs to be exposed must begin with capital letters.

    When the name starts with 1 capital letter,like:GetUserName, then the object using this form of identifier,Can be used by external package code(The external need to import this package first), this process is also called export.

  • Names that do not need to be exposed to the outside world start with lowercase letters.

    When the name starts with 1 lowercase letter,like:getUserName, thenObjectthat isNot visible outside the package, but inThe entire package interiorAllvisibleandAvailableof.

2. Package naming specifications

  • Keep the package name and directory name consistent. Right nowpackage maincorrespondmainfolder directory
  • Try to use some short and intuitive package names to avoid conflict with the standard library.
  • Package names are generally all lowercase words, without underscores or mixed cases.

Example:

package dao
package service

3. Document naming specifications

  • Try to use some short and intuitive file names.
  • File names are generally lowercase words, and underscores are used to separate the words.

Example:

customer_dao.go

4. Naming specifications for structures

  • The naming is short and intuitive.
  • Generally, camel nomenclature is used, and the first letter controls uppercase or lowercase according to the access situation.
  • structThe declaration and initialization are written in multiple lines.

Example:

type CustomerOrder struct {
    Name string
    Address string
}
order := CustomerOrder{"Tom", "Haidian District, Beijing"}

5. Naming specifications for interfaces/functions

  • The naming is short and intuitive.
  • Similar to the structure, the camel nomenclature is generally adopted, and the first letter controls uppercase or lowercase according to the access situation.
  • Functions starting with capital letters (public, visible outside the package) should be commented.
  • Declaration and initialization are written in multiple lines.
  • The naming of a single interface/function is generallyerThe ending is used as a suffix, for example:Reader, Writer

Example:

type Reader interface {
    Read(p []byte) (n int, err error)
}

VI. Naming specifications for variables

  • The naming is short and intuitive.

  • Similar to structures, functions/interfaces, the camel nomenclature is generally adopted, and the first letter controls uppercase or lowercase according to the access situation.

  • If you encounter a unique noun (abbreviation or abbreviation, such as:DNS), the following rules need to be followed:

    If the variable is private and the unique noun is the first word, all lowercase is:appService

    If the variable is public, all unique nouns are capitalized.

    If the variable isbooltype, then the name isHas, Is, Can, Allowbeginning.

Example:

var isExit bool
var hasConflict bool
var canManage bool
var allowGitHook bool

7. Constant naming specifications

  • The naming is short and intuitive.
  • Words are in all capitalizations and underlined words.
  • If it is a constant of an enum type, you need to create the corresponding type first.

Example:

type Scheme string		//Create corresponding types for enum type constantsconst {
    HTTP Scheme = "http"
    HTTPS Scheme = "https"
}

8. Naming specifications for error handling

Principles of error handling:

Any calls that return err cannot be discarded, and they are not discarded using _discarded, and must be processed.

When an error is received, either return err, or use log to record it and return as soon as possible.

Once an error occurs, return immediately and try not to use panic unless you know what to do when handling the error.

If the error description is used in English, it must be in lowercase, and there is no need to use punctuation and use an independent error stream for processing.

Example:

// Correct writing// If an error occursif err != nil {
    // Perform error handling    return //or continue}
// If no error occurs, enter the normal code
// Error writingif err != nil {
    // Perform error handling    return //or continue} else {
    // Normal code}

9. Naming specifications for unit tests

  • Generally, the test case function name isTestStarting, such as:TestExample
  • Each important function must first write a test case, and the test case and regular code are submitted together to facilitate regression testing.
  • Unit test file names are generally used in lowercase words and are connected with underscores. The naming example is:example_test.go

10. Predefined identifiers of the system

Apart fromKeep keywordsIn addition, the Golang language system also provides36 predefined identifiers,includeBasic data typesandSystem embedded functions(No need to introduce packages, functions that can be used directly).

Predefined identifiers Predefined identifiers Predefined identifiers Predefined identifiers Predefined identifiers Predefined identifiers
append bool byte cap close complex
complex64 complex128 uint16 copy false float32
float64 imag int int8 int16 uint32
int32 int64 itoa len make new
nil panic uint64 print println real
recover string true uint uint8 uintprt

Summarize

This is the end of this article about Golang's naming specifications and best practices. For more relevant Golang naming specifications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!