SoFunction
Updated on 2025-03-05

Go language structure Go range learning tutorial

text

We studied in the previous blogGo array, it requires that all elements are of the same data type. If you want to store different types of data, you need to use the structure-related knowledge.

Definition of structure: Stores the same or different types of data collections.

With experience in C, the structure is relatively easy to understand, and the syntax format is as follows:

type struct_variable_type struct {
   member definition
   member definition
   ...
   member definition
}

The keywords in the above syntax format arestructandtypestruct_variable_type It is the name of a structure. For example, we declare a structure that has a name, age, and gender.

package main
import "fmt"
// Declare structuretype People struct {
	name string
	age  int
	sex  int
}
func main() {
	//Use structure	people := People{
		"Eraser",
		18,
		0}
	(people)
}

When writing, you should pay attention to the syntax format. The code of the structure is recommended to be placed in one line, or the closing brace is immediately followed by the last element.

The element name can also be carried by using the structure, which is the following writing method.

//Use structurepeople := People{name: "Eraser", age: 18, sex: 0}
(people)

Accessing structure membersuseStructure. Member nameThat's right, of course, you can also use this method to assign values.

//Use structurevar people1 People
// var people2 People
 = "Eraser"
 = 18
 = 1
(people1)

Go Range

As a Python programmer, the keyword range is very familiar. In Go, the range keyword can be used in a for loop, used in an array to return the index and value of the element, and return key-value pairs in subsequently learned sets.

range The syntax format used for arrays is as follows:

for i,value := range a_array{
	// TODO
}

Combining the syntax format, write the following code:

package main
import "fmt"
var a_array = []int{1, 2, 3, 4, 5, 6, 7, 8}
func main() {
	for i, value := range a_array {
		("Index: %d, value: %d\n", i, value)
	}
}

Run the code to output the following information:

Index: 0, value: 1
Index: 1, value: 2
Index: 2, value: 3
Index: 3, value: 4
Index: 4, value: 5
Index: 5, value: 6
Index: 6, value: 7
Index: 7, value: 8

If you apply range to a string, you can output iteratively for each of its characters.

package main
import (
	"fmt"
)
func main() {
	var str string = "xiangpica"
	for k, v := range str {
		(k, string(v))
	}
}

The abovestrThe content in it is pure English.kValue +1 each time.

0 x
1 i
2 a
3 n
4 g
5 p
6 i
7 c
8 a

ifstrIncluded in Chinese,kThe value is +3 each time, the code is as follows:

func main() {
	var str string = "Eraser"
	for k, v := range str {
		(k, string(v))
	}
}

The output result is as follows:

0 oak
3 skin
6 Rub

If you are mixed with Chinese and English, the results will be even more interesting.

0 x
1 i
2 a
3 n
4 g
5 oaks
8 p
9 i
10 skins
13 c
14 a
15 Rub

There is actually a conclusion here.rangeIteration is Unicode, not bytes, and the return value is the index of UTF-8 encoding byte 1, so the index value may not be continuous.

When writing code, if you do not need indexes and only retain elements, you can use discarded placeholders. The code is as follows:

var str string = "Emeral Rubber"
for _, v := range str {
	(string(v))
}

Range related knowledge and other knowledge should also be involved when learning slices and collections. Here we refer to the experience in other languages. See you next article.

The above is the detailed content of the Go range learning tutorial. For more information about Go range structure, please pay attention to my other related articles!