Directory
- Data type
- Pointer
- Structural body
- Array
- Slice
- Methods of slicing
- Mapping
- function closures
- Conclusion
The third article in Go is about the data types in the go language, including pointers, structs, arrays, slices, mappings, function closures, and so on, each of which provides examples that can be run directly.
Data type pointers
The pointer holds the memory address of the value;
- (* operator) indicates the underlying value pointed to by the pointer;
- (& operator) generates a pointer to its operand;
Example:
func testPoint() { i := 20 p := &i fmt.Println(*p) *p = 1 fmt.Println(i)}
Interpretation: The value of *p is 1, I is the value of
- First output: Read I by pointer P
- Second output: Set I by pointer P
Structural body
Use the struct keyword to represent a struct.
A struct is a collection of fields that can be used to fetch values.
Example:
/*struct*/type user struct { name string age int}func testStruct() { x := user{"mingyu", 23} fmt.Println("对象", x) fmt.Println("姓名", x.name) x.name = "xiaona" fmt.Println("从新设置姓名", x.name) //通过指针访问 y := &x fmt.Println(*y) fmt.Println(y.age) //指定属性赋值 z := user{age: 11} fmt.Println("对象", z)}
Array
The type [n]t represents an array of values that have n T types.
Example:
//数组func testArray() { arr := [2]int{1, 2} fmt.Println("数组", arr) //指定下标赋值 arr[1] = 10 fmt.Println("数组", arr)}
Slice
The slice's gratitude is based on the array, and the array is dynamically obtained by specifying the subscript of the array.
such as: Arr[1:3] intercept the subscript 1 to 3 of the data [subscript before the Count, Intermediate: Split]:
- [1:3] Subscript 1 to 3; [: 2]: subscript 0 to 2 before; [1:] subscript 1.
The slice does not store any data, it simply describes a segment of the underlying array, so modifying the slice data will also change the array data.
The length and capacity of the slice s can be obtained by the expression Len (s) and the CAP (s), and the 0 value is nil.
Example:
//切片func testSection() { arr := [5]int{1, 2, 3, 4, 5} fmt.Println("数组", arr) simpleArr := arr[1:3] fmt.Println("切片", simpleArr) simpleArr[1] = 20 fmt.Println("修改切片后的数组", arr)}
Methods of slicing
Make
- Make Create slice B: = made ([]int, 0, 5)
- Parameter meaning: 1. Type, 2:len (length), 3:cap (volume);
Append
- Append slice array s = append (S, 2, 3, 4)
- Append element 2,3,4 to slice s tail
Range
- Used for looping through slices or mappings
- Each iteration returns two values: the first value is the subscript for the current element, and the second value is a copy of the element corresponding to the subscript
- The subscript or value can be assigned _ to ignore it
- If you only need to index, remove the value part
Example:
//切片方法func testSectionFunc() { arr := make([]string, 0, 0) fmt.Println(len(arr), cap(arr), arr) arr = append(arr, "陈", "明羽") fmt.Println(len(arr), cap(arr), arr) for i, v := range arr { fmt.Println(i, v) } fmt.Println("-----------") for _, v := range arr { fmt.Println(v) } fmt.Println("-----------") for i := range arr { fmt.Println(i) }}
Mapping
Maps a key to a value, that is, a map.
The Make function returns a mapping of the given type and initializes it with an alternate.
Example:
//映射func testMap() { m := make(map[string]user) m["1号"] = user{ "明羽", 23, } fmt.Println(m["1号"]) //还可以这么写,还是觉得这么写舒服 var mor = map[string]user{ "1号": { "明羽", 23, }, "2号": { "小娜", 23, }, } fmt.Println(mor)}
Related methods:
- Insert or modify elements in M: m[key] = Elem
- element: Elem = M[key]
- Vegetarian: Delete (m, key)
- Double assignment detects if a key exists: Elem, OK = M[key]
- If key is in M, OK is true; otherwise, OK is false.
- If key is not in the map, then Elem is the 0 value of the mapping element type.
- If Elem or OK is not yet declared, you can use the short variable declaration: elem, OK: = M[key]
function closures
Functions can be passed as values.
Example:
//函数闭包func testB() func(int) int { x := 10 return func(a int) int { return x + a }}// c 就是testB返回的方法c(i)就是调到x+afunc testA() { c := testB() for i := 0; i < 5; i++ { fmt.Println(c(i)) }}
Conclusion
First knowledge of Go language series
- [x] First knowledge of the go language
- [x] First knowledge of Go language: Grammar
- [x] Beginners Go language: Data type
- [] First knowledge of the go language: methods, interfaces and concurrency
X indicates to complete
Follow the public number
First knowledge of Go language: Data type