In programming languages, identifiers are used for exactly what their name suggests. In other words, identifiers are user-defined names for program elements. In Go, identifiers can be variable names, function names, constants, statement labels, package names, or types. Here's what you need to know about using identifiers in Golang.

For example:
package main
import "fmt"
func main() {
var name = "Quantrimang.com"
}
There are a total of three identifiers available in the above example:
- main : Package name
- main : Name of the function
- name : Name of the variable
Variable Definition Rules: There are some valid rules for defining valid Go variables. These rules need to be followed, otherwise we will get compilation errors.
- An identifier name must begin with a letter or an underscore(_). And it can contain the letters 'a-z' or 'A-Z' or the numbers 0-9 as well as the character '_'.
- The identifier name cannot begin with a digit.
- Identifier names are case sensitive.
- Keywords are not allowed as identifiers.
- There is no limit on the length of an identifier name, but an optimal length of 4 – 15 letters is recommended.
For example:
// Valid identifiers:
_geeks23
geeks
gek23sd
Geeks
geeKs
geeks_geeks
// Invalid identifiers:
212geeks
if
default
Note:
In Go, there are a number of pre-declared identifiers available for constants, types, and functions. These names are not reserved, you are allowed to use them in declarations. Here is a list of pre-declared identifiers:
Cho hằng số:
true, false, iota, nil
Cho các kiểu:
int, int8, int16, int32, int64, uint,
uint8, uint16, uint32, uint64, uintptr,
float32, float64, complex128, complex64,
bool, byte, rune, string, error
Cho các hàm:
make, len, cap, new, append, copy, close,
delete, complex, real, imag, panic, recover
- A variable represented by an underscore character (_) is called an empty identifier. It is used as an anonymous placeholder instead of a regular identifier and has special meaning in declarations, as an operand, and in assignments.
- A variable that is accessible from another package is called an exported identifier. Exported identifiers are those identifiers that satisfy the following conditions:
- The first character of the exported identifier name must be an uppercase Unicode letter.
- The identifier must be declared within a package block or as the name of a variable, function, type, or method within that package.
In the example below, file1.go contains an exported variable named ExportedVariable , which is accessible within the same file. It also imports the file2 package and accesses the exported variable AnotherExportedVariable from file2.go . By running go run file1.go , it will print the value of ExportedVariable (“Hello, World!”) from file1.go and the value of AnotherExportedVariable (“Greetings from file2!”) from file2.go . This demonstrates the concept of exported variables being accessible from another package in Go.
Example of file2:
//file2.go
package file2
// Exported variable
var AnotherExportedVariable = "Greetings from file2!"
Example of file1:
// file1.go
package main
import (
"fmt"
"github.com/yourusername/project/file2"
)
// Biến được xuất
var ExportedVariable = "Hello, World!"
func main() {
// Truy cập biến đã xuất trong cùng file
fmt.Println(ExportedVariable)
// Truy cập biến đã xuất từ gói khác
fmt.Println(file2.AnotherExportedVariable)
}
Result:
Hello, World!
Greetings from file2!