Gaaker Go

Golang Function naming convention

Some points worth noting about function naming

  • Function name cannot start with a number
  • Function names are case sensitive. Therefore, plus, Plus, and PLUS are different functions.
  • Use camel case naming for function names, do not use underscores, and do not include spaces.
  • The first letter is capitalized to distinguish between public functions and private functions. Public functions can be called outside the package, but private functions are not.

The following is a complete code example.

package main

import (
	"fmt"
)

func plus(a int, b int) {
    fmt.Println("a + b = ", a + b)
}
func Plus(a int, b int) {
    fmt.Println("a + b = ", a + b)
}
func PlusPlus(a int, b int) {
    fmt.Println("a + b = ", a + b)
}
func Plus123(a int, b int) {
    fmt.Println("a + b = ", a + b)
}
func main() {
	plus(1,1)
	Plus(1,1)
	PlusPlus(1,1)
	Plus123(1,1)
}

Below is the program output:

a + b =  2
a + b =  2
a + b =  2
a + b =  2
< Function naming convention Function naming convention >