slice // Slice, default value is nilmap // Map, default value is nilchan // Channel, default value is nil
2. Encoding & Strings
Go’s default encoding is UTF-8.
Unicode: A global character encoding standard with variable-length encoding. Multiple implementations exist, such as UTF-8 and UTF-16. Character lengths range from 1 to 4 bytes. English characters typically occupy 1 byte; Chinese characters occupy 2 bytes. Each character corresponds to a hexadecimal number. Use utf8.RuneCountInString() to get the length of a Unicode string.
UTF-8: A variable-length character encoding, one implementation of Unicode. English characters take 1 byte, Chinese characters take 3 bytes, emojis take 4 bytes.
Go has two types of characters: byte and rune.
byte: Alias of uint8, commonly used for handling ASCII characters. Represents an ASCII string. Use []byte(s) to convert string s into a byte array.
rune: Alias of int32, equivalent to int32. Typically used to represent a Unicode code point, capable of handling any character. Use []rune(s) to convert string s into a Unicode rune array.
string: A sequence of fixed-length characters, internally implemented as a byte array. Strings are immutable. When strings contain only letters and digits, they can be converted to []byte or []rune. For multi-byte encoded characters like Chinese, conversion to []rune is required. The most universal method is []rune(s).
Single quotes: Used to denote byte or rune types. Default is rune. Content inside single quotes is an ASCII character. Use len() to get the length of an ASCII string.
Double quotes: Denote a string literal that supports escape sequences but cannot span multiple lines.
Backticks: Used to create raw string literals, supporting no escape sequences and allowing multiline content. Commonly used for writing multi-line messages, HTML, regular expressions, etc.
append // Append elements to arrays or slices, returns modified array/sliceclose // Primarily used to close channelsdelete // Remove key-value pair from mappanic // Stop normal goroutine execution (used with recover for error handling)recover // Allows program-defined recovery from panic in goroutinesreal // Return real part of complex number (complex, real, imag for creating/manipulating complex numbers)imag // Return imaginary part of complex numbermake // Allocate memory, return the Type itself (only valid for slice, map, channel)new // Allocate memory, primarily for value types like int, struct, return pointer to typecap // Return maximum capacity of a type (only valid for slice and map)copy // Copy elements from one slice to another, return number of copied elementslen // Get length (e.g., string, array, slice, map, channel)print, println // Low-level print functions
Placeholder Description Example Output%v Default format of the value Printf("%v", people) {zhangsan}%+v Include field names when printing structs Printf("%+v", people) {Name:zhangsan}%#v Go syntax representation of the value Printf("#v", people) main.Human{Name:"zhangsan"}%T Go syntax representation of the type Printf("%T", people) main.Human%% Literal percent sign, not a value placeholder Printf("%%") %%t Boolean placeholder (true or false) Printf("%t", true) true%c Character placeholder, corresponding Unicode code point Printf("%c", 0x4E2D) 中%d Integer placeholder, decimal representation Printf("%d", 0x12) 18%s String placeholder (string type or []byte) Printf("%s", []byte("Go语言")) Go语言%p Pointer placeholder, hexadecimal with prefix 0x Printf("%p", &people) 0x4f57f0
III. Arrays, Slices, and Maps
1. Arrays
var array_variable_name [number_of_elements]type
Array length must be a constant; once defined, it cannot change. Elements within the array can be modified.
A flexible, variable-length sequence of elements of the same type. Slices are built on top of arrays and support automatic resizing. Slices are reference types, internally containing address, length (len), and capacity (cap).
var a []string// Declares a slice of length 0, type string
var a []string{} // Declares a slice of length 0, type string, initialized
var myslice = []string{"Beijing", "Shanghai", "Shenzhen"}
for i :=0; i < len(myslice); i++ {
fmt.Println("for loop:", myslice[i])
}
3. Maps
An unordered data structure based on key-value pairs. In Go, maps are reference types and must be initialized using make before use.
Syntax: map[key_type]value_type
Maps have a default initial value of nil. Memory must be allocated using the make() function. Note: cap does not apply to maps—cap returns the allocated space size of a slice/array. To get the map’s capacity, use len().
userInfo :=map[string]string{
"username": "xiaoli",
"password": "1234556",
}
for k, v :=range userInfo {
fmt.Println(k, v, len(userInfo))
}
IV. Pointers
In Go, reference types require declaration and memory allocation; otherwise, values cannot be stored. For value types, memory is automatically allocated during declaration.
Pointer address:&aPointer dereference:*&aPointer type:&a→ *int&gets address, * dereferences to value
new: Allocates memory for a type and returns a pointer. Useful for instantiating structs, returning the pointer address of the struct.
make: Only used for creating memory for chan, map, and slice. Returns the actual type, not a pointer.
V. Structs
Go does not have classes or class inheritance. Instead, it uses structs combined with interfaces to achieve higher extensibility and flexibility compared to traditional object-oriented programming.
1. Ways to Instantiate Structs
Direct assignment (var), new, and key-value initialization.