Go language structure
Arrays in Go language can store the same type of data, but in structures we can define different data types for different items.
A structure is a data collection composed of a series of data of the same type or different types.
The structure represents a record, such as saving book records in a library. Each book has the following attributes:
Title: Title
Author: Author
Subject: Subject
ID: Book ID
Define structure
Structure definition requires the use of type and struct statements. The struct statement defines a new data type, and the structure has one or more members. 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 the structure type is defined, it can be used for variable declaration. The syntax format is as follows:
variable_name := structure_variable_type {value1, value2...valuen}
Accessing structure members
If you want to access the structure members, you need to use the dot (.) operator, the format is: "structure.member name".
Structure type variables are defined using the struct keyword. The example is as follows:
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.php.cn" Book1.subject = "Go 语言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "www.php.cn" 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 execution result of the above example is:
Book 1 title : Go 语言 Book 1 author : www.php.cn Book 1 subject : Go 语言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.php.cn Book 2 subject : Python 语言教程 Book 2 book_id : 6495700
Structure as function parameter
You can pass structure types as parameters to functions just like other data types. And access the structure variable in the way of the above 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.php.cn" Book1.subject = "Go 语言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "www.php.cn" 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 execution result of the above example is:
Book title : Go 语言 Book author : www.php.cn Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.php.cn Book subject : Python 语言教程 Book book_id : 6495700
Structure pointer
You can define a pointer to the structure The pointer is similar to other pointer variables, and the format is as follows:
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 in front of the structure variable:
struct_pointer = &Book1;
Use the structure pointer to access the structure members and use the "." operator:
struct_pointer.title;
Next let We 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 /* Declare Book1 of type Book */ var Book2 Books /* Declare Book2 of type Book */ /* book 1 描述 */ Book1.title = "Go 语言" Book1.author = "www.php.cn" Book1.subject = "Go 语言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "www.php.cn" 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 execution result of the above example is:
Book title : Go 语言 Book author : www.php.cn Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.php.cn Book subject : Python 语言教程 Book book_id : 6495700