Structure of the basics of Go language (Spring Chapter)

Release: 2023-07-24 17:40:13
forward
1002 people have browsed it

Recall map

We have said before that the map in Go language is a key value The right way to store data, like this. For those who don’t remember, please click: An article will take you to understand the basics of Go language map and the basic map supplement of Go language.

//方式一
var student = map[string]string{
    "Name": "张三",
    "Age":  "18",
}
//方式二
var student2 = make(map[string]string, 10)
student2["Name"] = "张三"
student2["Age"] = "18"
Copy after login

But this seems to have a drawback, I don’t know how manykey , and the value type is fixed.

##Theoretically speaking, key Age The corresponding value should be of type int , and there are some other subtle problems that are not easy to solve through map.

To solve these problems, the structure type was introduced.


##Foreplay supplement

Before starting the structure, first Look at two strange knowledge points.


自定义类型

代码

type 自定义类型名 类型名
例:
type NewInt int
Copy after login

完整代码

package main


import "fmt"


type NewInt int


func main() {
    var n1 NewInt = 1
    fmt.Println(n1)//结果为1
}
Copy after login

如果要是理解的话,可以理解为NewInt包含了int的功能。

这里可以把NewInt当作int来使用。

注:NewInt是一个新的类型,它包含int,并不等于int


类型别名

代码

type 类型别名 = 类型名
例:
type Nint = int
Copy after login

完整代码

package main


import "fmt"


type Nint = int


func main() {
    var n1 Nint = 1
    fmt.Println(n1)//1
}
Copy after login

自定义类型和类型别名区别

可能猛一看,感觉自定义类型和类型别名似乎一样,但是其实是不太一样的。

代码

package main


import "fmt"


type Nint1 int   //自定义类型
type Nint2 = int //类型别名


func main() {
    var n1 Nint1 = 1
    var n2 Nint2 = 1
    fmt.Printf("n1类型:%T,n2类型:%T", n1, n2)
}
Copy after login

执行结果。

Structure of the basics of Go language (Spring Chapter)

Conclusion:Custom types are really custom types. The types have changed. Type aliases only change the type name, but the essence remains the same.


## Structure

Go language structure, Classes in languages ​​such as Java and Python are no longer A simple structure is so simple.

Structurebelongs to basic data type.

The memory diagram is roughly as follows.

Structure of the basics of Go language (Spring Chapter)


Structure definition

Definition of structure The keywords type and struct are required for the body.

语法

type 结构体名 struct {
    字段1 字段类型1
    字段2 字段类型2
    ...
}
Copy after login

示例,通过结构体描述一个学生。

type Student struct {
    Name   string
    Age    int
    Height int
    Weight int
    phone string
}
Copy after login

注:如果字段类型是相同的,可以写在同一行。

type Student struct {
  Name                string
  Age, Height, Weight int
  phone               string
}
Copy after login

结构体初始化

方式一,赋值时初始化

func main() {
  var s1 = Student{
    Name:   "张三",
    Age:    18,
    Height: 180,
    Weight: 120,
    phone:  "6666666",
  }
  fmt.Println(s1)
}
Copy after login

方式二,先声明,后赋值

func main() {
  var s1 Student
  s1.Name = "张三"
  s1.Age = 18
  s1.Height = 180
  s1.Weight = 120
  s1.phone = "66666"
}
Copy after login

两个执行结果。

Structure of the basics of Go language (Spring Chapter)


匿名结构体

有时候我们的函数可能会要求传入一个结构体,但是你又不想定义,就想临时用一下,赶紧传参得了。

这时候可以考虑匿名结构体。

方式一,先声明,后赋值

func main() {
  var car struct {
    Name   string;
    CarNum string
  }
  car.Name = "QQ"
  car.CarNum = "京6666"
  fmt.Println(car) //{QQ 京6666}
}
Copy after login

方式二,声明+初始化

func main() {
  var car = struct {
    Name   string;
    CarNum string
  }{
    Name:   "QQ",
    CarNum: "京6666",
  }


  fmt.Println(car) //{QQ 京6666}
}
Copy after login

两个执行结果。

Structure of the basics of Go language (Spring Chapter)


通过&方式初始化结构体

通过&的方式初始化,性能会提高一点,因为返回的是第一个的指针

但是操作过程跟上述一样,Go已经封装好了。

代码

func main() {
  //方式一,等于一个空&结构体在赋值
  var s1 = &Student{}
  s1.Name = "张三"
  //...
  //方式二,直接赋值
  var s2 = &Student{
    Name:   "",
    Age:    0,
    Height: 0,
    Weight: 0,
    phone:  "",
  }
  //方式三不可以
  //var s3 &Student//error
}
Copy after login

使用&的方式基本跟原来一样,但是方式三不行。


&初始化结构体函数注意事项

如果使用&的方式,那函数参数也要变一下的。

package main


import "fmt"


type Student struct {
  Name                string
  Age, Height, Weight int
  phone               string
}


func sayStudent1(s Student) {
  fmt.Println(s)
}
func sayStudent2(s *Student) {
  //如果穿的是结构体地址,那么接收就需要用*
  fmt.Println(s)
}
func main() {
  var s1 = Student{
    Name:   "111",
    Age:    0,
    Height: 0,
    Weight: 0,
    phone:  "1111",
  }
  var s2 = &Student{
    Name:   "2222",
    Age:    0,
    Height: 0,
    Weight: 0,
    phone:  "2222",
  }
  sayStudent1(s1)
  sayStudent2(s2)
}
Copy after login

执行结果。

Structure of the basics of Go language (Spring Chapter)


关于结构体默认值

代码

func main() {
  var s1 = Student{}
  fmt.Println(s1)
}
Copy after login

执行结果。

Structure of the basics of Go language (Spring Chapter)

在操作结构体时,即使没有赋值,也会有默认值,所以不用担心会报错。

int默认值是0,string默认值是"",等。

The above is the detailed content of Structure of the basics of Go language (Spring Chapter). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!