SoFunction
Updated on 2025-03-05

Detailed explanation of the usage and examples of Go language infrastructure

Overview

A structure is a collection of data composed of a series of data of the same type or different types.

grammar

Define structure [identifies the name of the custom structure, which cannot be repeated in the same package]

type Structure name struct {
  Fields1: Fields1Value of,
  Fields2: Fields2Value of,
  ......
}

example

//studenttype Student struct {
  Name string   //Name  Age int       //age  Sex bool      //Gender true:Male false:Female}

Three forms of structure definition

The first type [basic instantiation]

var stu Student

The second type of [Pointer type structure]

var stu *Student = new(Student)

The third type [taking the address of the structure instantiation, through the operation of &]

var stu *Student = &Student{}

Initialize the structure

Key-value pair initialization structure

Key values ​​are separated by: separated by key values; key values ​​are separated by:

Variable name:= Structure type name{
Field 1: The value of field 1,
Field 2: The value of field 2,
    ......
}

example

stu3 := Student{
  Name: "Li Si",
  Age: 18}

Value list fills structure

There are no fields, according to the sequence, all must be filled

Variable name:= Structure type name{
The value of field 1,
The value of field 2,
    ......
}

stu4 := Student{
  "Wang Wu",
  18,
  true,
}

Anonymous structure

Assign values ​​simultaneously when defining structures

Variable name:= struct{
Field 1: Field type 1,
Field 2: Field type 2,
    ......
}{
// Field value initialization
Field 1: The value of field 1,
Field 2: The value of field 2,
    ......
}

stu5 := struct {
  Name string
  Age  int
}{
  Name: "Wang Wu",
  Age:  18,
}

Accessing structure members

Use the "." point to the symbol ".

Structure.field

var stu Student
="Zhang San"         //Assignment=18
=true

()   //access

Structure as function parameter

func Function name([Structural variables、Structure pointer variable]){}
func printStudent(stu Student) {}
func printStudent(stu *Student) {}

Structural pointer

Use the structure pointer to access the structure members, using the "." operator.

var Variable name *Structure name
var stu1 *Student = new(Student)
 = "Li Si"
 = 20
 = false

Add structure method

Add a method to the structure, add a bracket between the func and the method name, and add a pointer reference to the structure [can also be a value reference]

func ([Structure name]) Method name([Parameter list])[Return to the value list]{}

example

var stu Student
 = "Zhang San"
 = 18
 = false

//Call()

//Add structure methodfunc (stu Student) sayHi() {
	(, "Hi")
}

Summarize

golang is a non-object-oriented language. It can also be said that the structures in go language are similar to classes in java, but it is obviously lacking in the characteristics of inheritance polymorphism and other OO.

Pointer variables access structure members through . If they are C or C++, they must be accessed through *. This is an optimization by Go to it.

Example

package main
import "fmt"
//Define the structure [Identify the name of the custom structure, it cannot be repeated in the same package]type Student struct {
	Name string
	Age  int
	Sex  bool
}
func main() {
	//Three forms of struct definition	var stu Student
	 = "Zhang San"
	 = 18
	 = false
	(stu)
	var stu1 *Student = new(Student)
	 = "Li Si"
	 = 20
	 = false
	(stu1)
	var stu2 *Student = &Student{}
	 = "Wang Wu"
	 = 55
	 = true
	(stu2)
	//Initialize the structure	stu3 := Student{
		Name: "Li Si",
		Age:  18}
	(stu3)
	stu4 := Student{
		"Wang Wu",
		18,
		true,
	}
	(stu4)
	//Anonymous structure	stu5 := struct {
		Name string
		Age  int
	}{
		Name: "Wang Wu",
		Age:  18,
	}
	(stu5)
	// printStudent(stu)
	printStudent(stu1)
	()
}
//Add structure methodfunc (stu Student) sayHi() {
	(, "Hi")
}
// func printStudent(stu Student) {
// 	(, , )
// }
func printStudent(stu *Student) {
	(, , )
}

The above is a detailed explanation of the usage and examples of the Go language infrastructure. For more information about Go language structures, please pay attention to my other related articles!