[Go] Go Language Tutorial--GO Language Structure (13)

Past review:


Arrays in Go language can store data of the same type, but in structures we can define different data types for different items.

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

A structure represents a record, such as saving a book record in a library, and each book has the following attributes:

  • Title: title
  • Author : author
  • Subject: subject
  • ID: Book ID

Define the structure

Structure definitions require the use of 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 struct_variable_type struct {
    
    
   member definition
   member definition
   ...
   member definition
}

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

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

Examples are as follows:

example

package main

import "fmt"

type Books struct {
    
    
   title string
   author string
   subject string
   book_id int
}


func main() {
    
    

    // 创建一个新的结构体
    fmt.Println(Books{
    
    "Go 语言", "www.demo.com", "Go 语言教程", 6495407})

    // 也可以使用 key => value 格式
    fmt.Println(Books{
    
    title: "Go 语言", author: "www.demo.com", subject: "Go 语言教程", book_id: 6495407})

    // 忽略的字段为 0 或 空
   fmt.Println(Books{
    
    title: "Go 语言", author: "www.demo.com"})
}

The output is:

{
    
    Go 语言 www.demo.com Go 语言教程 6495407}
{
    
    Go 语言 www.demo.com Go 语言教程 6495407}
{
    
    Go 语言 www.demo.com  0}

access structure members

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

Structure.Member name"
structure type variables are defined using the struct keyword, examples are as follows:

example

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.demo.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.demo.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)
}

The above example execution results are as follows:

Book 1 title : Go 语言
Book 1 author : www.demo.com
Book 1 subject : Go 语言教程
Book 1 book_id : 6495407
Book 2 title : Python 教程
Book 2 author : www.demo.com
Book 2 subject : Python 语言教程
Book 2 book_id : 6495700

Structs as function parameters

You can pass struct types as arguments to functions just like any other data type. And access the structure variable in the way of the above example:

example

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.demo.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.demo.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 above example execution results are as follows:

Book title : Go 语言
Book author : www.demo.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.demo.com
Book subject : Python 语言教程
Book book_id : 6495700

structure pointer

You can define pointers to structures 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 structure variable address, you can place the & symbol before the structure variable:

struct_pointer = &Book1

To access structure members using a structure pointer, use the "." operator:

struct_pointer.title

Next, let us use the structure pointer to rewrite the above example, the code is as follows:

example

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.demo.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.demo.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 above example execution results are as follows:

Book title : Go 语言
Book author : www.demo.com
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.demo.com
Book subject : Python 语言教程
Book book_id : 6495700

おすすめ

転載: blog.csdn.net/u011397981/article/details/131725092