Detailed explanation of Struct (structure) in Golang

青灯夜游
Release: 2022-11-17 20:49:12
forward
3108 people have browsed it

Detailed explanation of Struct (structure) in Golang

The Go language provides support for struct; struct, the Chinese translation is called structure, which, like arrays, is a composite type , not a reference type. [Related recommendations: Go Video Tutorial]

The struct in Go language is similar to the struct in C language or the class (class) in other object-oriented programming languages. Fields (attributes) can be defined ) and methods, but they are also very different and require in-depth study to distinguish the differences between them.

Pay attention to the difference between composite types and reference types. This should also be the difference between value passing and reference passing.

Definition

Use the struct keyword to define a structure. The members in the structure are called fields or attributes of the structure.

type Member struct {
    id          int
    name, email string
    gender, age int
}
Copy after login

In the above code, we define a structure containing 5 fields. You can see that the same types name and email, gender and age are defined in the same line, but a better programming habit is to define only one field in each line, such as:

type Member struct {
    id     int
    name   string
    email  string
    gender int
    age    int
}
Copy after login

Of course, the structure can also not contain any fields. , called empty structure, struct{} represents an empty structure. Note that it does not make sense to directly define an empty structure, but in concurrent programming, communication between channels can be used A struct{} acts as a semaphore.

ch := make(chan struct{})
ch <- struct{}{}
Copy after login

Usage

In the above example, we defined the Member structure type, and then we can create variables of this custom type.

Define variables directly. This method of use does not assign an initial value to the field, so all fields will be automatically assigned zero values ​​of their own type. For example, the value of name is a null character. String "", the value of age is 0.

var m1 Member//所有字段均为空值
Copy after login

Use literals to create variables. In this way, you can assign initial values ​​to the members of the structure in braces. There are two ways to assign initial values. One is Assign values ​​in the order in which the fields are in the structure. m2 in the code below uses this method. This method requires that all fields must be assigned values, so if there are too many fields, each field must be assigned a value. It will be very cumbersome. The other method is to use the field name to specify the field assignment, such as the creation of the variable m3 in the following code. Using this method, for other unspecified fields, use the zero value of the field type. value as the initialization value.

var m2 = Member{1,"小明","xiaoming@163.com",1,18} // 简短变量声明方式:m2 := Member{1,"小明","xiaoming@163.com",1,18}
var m3 = Member{id:2,"name":"小红"}// 简短变量声明方式:m3 := Member{id:2,"name":"小红"}
Copy after login

Access fields

You can access fields in the structure type through variable names, using commas (.), or assign values ​​to fields, or you can Perform the address (&) operation on the field.

fmt.Println(m2.name)//输出:小明
m3.name = "小花"
fmt.Println(m3.name)//输出:小花

age := &m3.age
*age = 20
fmt.Println(m3.age)//20
Copy after login

Pointer structure

Structures, like arrays, are passed by value. For example, when an array or structure is passed as an actual parameter to the formal parameter of a function, a copy will be made. Therefore, in order to improve performance, arrays are generally not passed directly to functions, but slices (reference types) are used instead. When passing structures to functions, pointer structures can be used.

Pointer structure, that is, a pointer to a structure. When declaring a structure variable, add an * sign before the structure type to declare a pointer to the structure, such as:

Note that the pointer type is a reference type. When declaring a structure pointer, if it is not initialized, the initial value is nil. Only after initialization can the field be accessed or assigned a value.

var m1 *Member
m1.name = "小明"//错误用法,未初始化,m1为nil

m1 = &Member{}
m1.name = "小明"//初始化后,结构体指针指向某个结构体地址,才能访问字段,为字段赋值。
Copy after login

In addition, using Go's built-in new() function, you can allocate memory to initialize the structure and return the allocated memory pointer. Because it has been initialized, you can directly access the fields.

var m2 = new(Member)
m2.name = "小红"
Copy after login

We know that if you transfer the structure to a function, you will only make a copy of the structure. If you modify the field value of the structure within the function, the outer structure will not be affected, and if you transfer the structure If a pointer is passed to a function, any modifications made to the structure using the pointer in the function will affect the structure pointed to by the pointer.

func main() {
    m1 := Member{}
    m2 := new(Member)
    Change(m1,m2)
    fmt.Println(m1,m2)
}

func Change(m1 Member,m2 *Member){
    m1.Name = "小明"
    m2.Name = "小红"
}
Copy after login

Visibility

In the above example, we defined the first letter of the structure field name to be lowercase, which means that these fields are not visible outside the package, so Cannot be accessed in other packages, only allowed within the package.

In the following example, we declare Member in the member package, and then create a variable in the main package. However, because the fields of the structure are not visible outside the package, we cannot assign an initial value to the field and cannot press Whether the field is assigned by index or assigned, a panic error will occur.

package member
type Member struct {
    id     int
    name   string
    email  string
    gender int
    age    int
}

package main

fun main(){
    var m = member.Member{1,"小明","xiaoming@163.com",1,18}//会引发panic错误
}
Copy after login

Therefore, if you want to access the fields of a structure in another package in one package, it must be a variable starting with a capital letter, that is, an exported variable, such as:

type Member struct {
    Id     int
    Name   string
    Email  string
    Gender int
    Age    int
}
Copy after login

Tags

When defining a structure field, in addition to the field name and data type, you can also use backticks to declare meta-information for the structure field. This meta-information is called Tag and is used to associate it with the field during the compilation phase. ,For example, we modify the structure in the above example to:

type Member struct {
    Id     int    `json:"id,-"`
    Name   string `json:"name"`
    Email  string `json:"email"`
    Gender int    `json:"gender,"`
    Age    int    `json:"age"`
}
Copy after login

上面例子演示的是使用encoding/json包编码或解码结构体时使用的Tag信息。

Tag由反引号括起来的一系列用空格分隔的key:"value"键值对组成,如:

Id int `json:"id" gorm:"AUTO_INCREMENT"`
Copy after login

特性

下面总结几点结构体的相关特性:

值传递

结构体与数组一样,是复合类型,无论是作为实参传递给函数时,还是赋值给其他变量,都是值传递,即复一个副本。

没有继承

Go语言是支持面向对象编程的,但却没有继承的概念,在结构体中,可以通过组合其他结构体来构建更复杂的结构体。

结构体不能包含自己

一个结构体,并没有包含自身,比如Member中的字段不能是Member类型,但却可能是*Member。

方法

在Go语言中,将函数绑定到具体的类型中,则称该函数是该类型的方法,其定义的方式是在func与函数名称之间加上具体类型变量,这个类型变量称为方法接收器,如:

注意,并不是只有结构体才能绑定方法,任何类型都可以绑定方法,只是我们这里介绍将方法绑定到结构体中。

func setName(m Member,name string){//普通函数
    m.Name = name
}

func (m Member)setName(name string){//绑定到Member结构体的方法
    m.Name = name
}
Copy after login

从上面的例子中,我们可以看出,通过方法接收器可以访问结构体的字段,这类似其他编程语言中的this关键词,但在Go语言中,只是一个变量名而已,我们可以任意命名方法接收器

调用结构体的方法,与调用字段一样:

m := Member{}
m.setName("小明")
fmt.Println(m.Name)//输出为空
Copy after login

上面的代码中,我们会很奇怪,不是调用setName()方法设置了字段Name的值了吗?为什么还是输出为空呢?

这是因为,结构体是值传递,当我们调用setName时,方法接收器接收到是只是结构体变量的一个副本,通过副本对值进行修复,并不会影响调用者,因此,我们可以将方法接收器定义为指针变量,就可达到修改结构体的目的了。

func (m *Member)setName(name string){/将Member改为*Member
    m.Name = name
}

m := Member{}
m.setName("小明")
fmt.Println(m.Name)//小明
Copy after login

方法和字段一样,如果首字母为小写,则只允许在包内可见,在其他包中是无法访问的,因此,如果要在其他包中访问setName,则应该将方法名改为SetName

组合

我们知道,结构体中并没有继承的概念,其实,在Go语言中也没有继承的概念,Go语言的编程哲学里,推荐使用组合的方式来达到代码复用效果。

什么是组合

组合,可以理解为定义一个结构体中,其字段可以是其他的结构体,这样,不同的结构体就可以共用相同的字段。

注意,在记得我们前面提过的,结构体不能包含自身,但可能包含指向自身的结构体指针。

例如,我们定义了一个名为Animal表示动物,如果我们想定义一个结构体表示猫,如:

type Animal struct {
    Name   string  //名称
    Color  string  //颜色
    Height float32 //身高
    Weight float32 //体重
    Age    int     //年龄
}
//奔跑
func (a Animal)Run() {
    fmt.Println(a.Name + "is running")
}
//吃东西
func (a Animal)Eat() {
    fmt.Println(a.Name + "is eating")
}

type Cat struct {
    a Animal
}

func main() {
    var c = Cat{
	    a: Animal{
            Name:   "猫猫",
            Color:  "橙色",
            Weight: 10,
            Height: 30,
            Age:    5,
        },
    }
    fmt.Println(c.a.Name)
    c.a.Run()
}
Copy after login

可以看到,我们定义Cat结构体时,可以把Animal结构体作为Cat的字段。

匿名字段

上面的例子,我们看到,把Animal结构体作为Cat的字段时,其变量名为a,所以我们访问Animal的方法时,语法为c.a.Run(),这种通过叶子属性访问某个字段类型所带的方法和字段用法非常繁琐。

Go语言支持直接将类型作为结构体的字段,而不需要取变量名,这种字段叫匿名字段,如:

type Lion struct {
	Animal //匿名字段
}

func main(){
    var lion = Lion{
        Animal{
            Name:  "小狮子",
            Color: "灰色",
        },
    }
    lion.Run()
    fmt.Println(lion.Name)
}
Copy after login

通过上面例子,可以看到,通过匿名字段组合其他类型,而后访问匿名字段类型所带的方法和字段时,不需要使用叶子属性,非常方便。

小结

在Go语言编程中,结构体大概算是使用得最多的数据类型了,通过定义不同字段和方法的结构体,抽象组合不同的结构体,这大概便是Go语言中对面向对象编程了。

原文地址:https://juejin.cn/post/6844903814168838151

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Detailed explanation of Struct (structure) in Golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template