Identifiers in Go

  • In the Go language, an identifier can be a variable name, function name, constant, statement label, package name, or type.
package main
import "fmt"
 
func main() {
 
 var name = "CRUX"
 
}

There is a total of three identifiers available in the above example:

  • main: Name of the package
  • main: Name of the function
  • name: Name of the variable

Rules for Defining Identifiers:

  • The name of the identifier must begin with a letter or an underscore(). And the names may contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘’.
  • The name of the identifier should not start with a digit.
  • The name of the identifier is case-sensitive.
  • Keywords are not allowed to be used as an identifier name.
  • There is no limit on the length of the name of the identifier, but it is advisable to use an optimum length of 4 – 15 letters only.

-In the Go language, there are some predeclared identifiers available for constants, types, and functions. These names are not reserved, you are allowed to use them in the declaration. Following is the list of predeclared identifiers:

# For Constants:
true, false, iota, nil
 
# For Types:
int, int8, int16, int32, int64, uint,
uint8, uint16, uint32, uint64, uintptr,
float32, float64, complex128, complex64,
bool, byte, rune, string, error
 
# For Functions:
make, len, cap, new, append, copy, close,
delete, complex, real, imag, panic, recover

The identifier represented by the underscore character( _ ) is known as a blank identifier. It is used as an anonymous placeholder instead of a regular identifier, and it has a special meaning in declarations, as an operand, and in assignments.

package main
 
import (
    "fmt"
)
 
func main() {
// Assigning the first return value to _ (blank identifier)
    _, b := myFunction()
    fmt.Println(b)
}
 
func myFunction() (int, string) {
    return 42, "Hello, World!"
}

In this code, the first return value from myFunction() is ignored using the _ identifier, while the second return value is assigned to b.