Detailed explanation of Go language structure

The structure is a data collection composed of a series of data of the same type or different types.
Arrays in Go language can store the same type of data, but in the structure we can define different data types for different items.

Structure definition

Structure definitions need to use type and struct statements.
The struct statement defines a new data type with one or more members in the structure.
The type statement sets the name of the structure.
The format of the structure is as follows:

type test struct {
    
    
   member definition
   member definition
   ...
   member definition
}

Once the structure type is defined, it can be used for variable declarations. The syntax format is as follows:

variable_name := test {
    
    value1, value2...valuen}
或
variable_name := test {
    
     key1: value1, key2: value2..., keyn: valuen}

Example:

package main


import "fmt"


type Books struct {
    
    

   title string

   author string

   subject string

   book_id int

}

func main() {
    
    

    // 创建一个新的结构体

    fmt.Println(Books{
    
    "Go 语言", "zhangsan", "Go 语言教程", 34324})

    // 也可以使用 key => value 格式

    fmt.Println(Books{
    
    title: "Go 语言", author: "zhangsan", subject: "Go 语言教程", book_id: 878756})

    // 忽略的字段为 0 或 空

   fmt.Println(Books{
    
    title: "Go 语言", author: "zhangsan"})

}

Access structure member

If you want to access the members of the structure, you need to use a dot. The operator
format is:

Structure. Member name

Structure type variables are defined using the struct keyword

package main



import "fmt"



type Books struct {
    
    

   title string

   author string

   subject string

   book_id int

}

func main() {
    
    

   var Book1 Books        /* 声明 Book1 为 Books 类型 */

   var Book2 Books        /* 声明 Book2 为 Books 类型 */



   /* book 1 描述 */

   Book1.title = "Go 语言"

   Book1.author = "www.runoob.com"

   Book1.subject = "Go 语言教程"

   Book1.book_id = 6495407



   /* book 2 描述 */

   Book2.title = "Python 教程"

   Book2.author = "www.runoob.com"

   Book2.subject = "Python 语言教程"

   Book2.book_id = 6495700



   /* 打印 Book1 信息 */

   fmt.Printf( "Book 1 title : %s\n", Book1.title)

   fmt.Printf( "Book 1 author : %s\n", Book1.author)

   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)

   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)



   /* 打印 Book2 信息 */

   fmt.Printf( "Book 2 title : %s\n", Book2.title)

   fmt.Printf( "Book 2 author : %s\n", Book2.author)

   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)

   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)

}

This is very similar to calling in JAVA. After defining the object, encapsulating the member variable, you can use. To call the member variable.
But the go language only supports encapsulation, and does not support inheritance and polymorphism, which is different from JAVA.

The running result is:

Book 1 title : Go 语言

Book 1 author : www.runoob.com

Book 1 subject : Go 语言教程

Book 1 book_id : 6495407

Book 2 title : Python 教程

Book 2 author : www.runoob.com

Book 2 subject : Python 语言教程

Book 2 book_id : 6495700

The structure type is passed to the function as a parameter

The pointer is not used to pass by value, and the source address can be changed by adding the pointer. Note that the pointer is used but the.

package main


import "fmt"



type Books struct {
    
    

   title string

   author string

   subject string

   book_id int

}

func main() {
    
    

   var Book1 Books        /* 声明 Book1 为 Books 类型 */

   var Book2 Books        /* 声明 Book2 为 Books 类型 */


   /* book 1 描述 */

   Book1.title = "Go 语言"

   Book1.author = "www.runoob.com"

   Book1.subject = "Go 语言教程"

   Book1.book_id = 6495407


   /* book 2 描述 */

   Book2.title = "Python 教程"

   Book2.author = "www.runoob.com"

   Book2.subject = "Python 语言教程"

   Book2.book_id = 6495700


   /* 打印 Book1 信息 */

   printBook(Book1)


   /* 打印 Book2 信息 */

   printBook(Book2)

}


func printBook( book Books ) {
    
    

   fmt.Printf( "Book title : %s\n", book.title)

   fmt.Printf( "Book author : %s\n", book.author)

   fmt.Printf( "Book subject : %s\n", book.subject)

   fmt.Printf( "Book book_id : %d\n", book.book_id)

}

You can directly take a as the structure address, and such a is also compatible with ordinary type operations, so it is a common situation

code show as below:

package main

import "fmt"

type test struct{
    
    
  Name string
  Age  int
}

func A(t* test){
    
    
  t.Age = 15
}

func main(){
    
     
  a:=&test{
    
    
   Name:"john",
   Age: 19,
}
  A(a)

  fmt.Println(a) 
   a.Age=20
  fmt.Println(a) 
}

Structure pointer

Defining a pointer to a structure is similar to other pointer variables in the following format:

var struct_pointer *Books

The pointer variable defined above can store the address of the structure variable. To view the address of the structure variable, the & symbol can be placed before the structure variable:

struct_pointer = &Book1

Use structure pointers to access structure members, use the "." operator:

struct_pointer.title

Use the structure pointer to rewrite the above example, the code is as follows:

package main


import "fmt"


type Books struct {
    
    

   title string

   author string

   subject string

   book_id int

}


func main() {
    
    

   var Book1 Books       

   var Book2 Books        



   /* book 1 描述 */

   Book1.title = "Go 语言"

   Book1.author = "www.runoob.com"

   Book1.subject = "Go 语言教程"

   Book1.book_id = 6495407



   /* book 2 描述 */

   Book2.title = "Python 教程"

   Book2.author = "www.runoob.com"

   Book2.subject = "Python 语言教程"

   Book2.book_id = 6495700



   /* 打印 Book1 信息 */

   printBook(&Book1)


   /* 打印 Book2 信息 */

   printBook(&Book2)

}


func printBook( book *Books ) {
    
    

   fmt.Printf( "Book title : %s\n", book.title)

   fmt.Printf( "Book author : %s\n", book.author)

   fmt.Printf( "Book subject : %s\n", book.subject)

   fmt.Printf( "Book book_id : %d\n", book.book_id)

}

The running result is:

Book title : Go 语言

Book author : www.runoob.com

Book subject : Go 语言教程

Book book_id : 6495407

Book title : Python 教程

Book author : www.runoob.com

Book subject : Python 语言教程

Book book_id : 6495700

Anonymous structure

package main


import "fmt"


func main(){
    
     

   a:=struct{
    
    
   Name string
   Age int
   }
   {
    
    
    Name:"joe",
    Age:19,
   }
   fmt.Println(a)
}

Structure nesting

package main

import "fmt"

type human struct{
    
    
   Sex int
}

type teacher struct{
    
    
  human
  Name  string
  Age  int
}

type student struct{
    
    
  human
  Name   string
  Age  int
}

func main(){
    
     

   a:=student{
    
    Name:"joe",Age:19,human:human{
    
    Sex:0}};
   b:=teacher{
    
    Name:"joe",Age:20,human:human{
    
    Sex:0}};
   
   fmt.Println(a,b)
   a.Sex=1
   fmt.Println(a,b)
}

Guess you like

Origin blog.csdn.net/zp17834994071/article/details/108748143