Go language structs
In Go, arrays can store data of the same type, but in structs we can define different data types for different items.
A struct is a data collection composed of a series of data of the same type or different types.
A struct represents a record, such as saving library book records. Each book has the following properties:
- Title: Title
- Author: Author
- Subject: Subject
- ID: Book ID
Define struct
Defining a struct requires the type and struct statements. The struct statement defines a new data type, and the struct has one or more members. The type statement sets the name of the struct. The format of a struct is as follows:
type struct_variable_type struct {
member definition
member definition
...
member definition
}
Once a struct type is defined, it can be used for variable declarations. The syntax format is as follows:
variable_name := structure_variable_type {value1, value2...valuen}
或
variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}
Example as follows:
Example
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// Create a new struct
fmt.Println(Books{"Go language", "www.example.com", "Go language tutorial", 6495407})
// You can also use the key => value format
fmt.Println(Books{title: "Go language", author: "www.example.com", subject: "Go language tutorial", book_id: 6495407})
// Ignored fields are 0 or empty
fmt.Println(Books{title: "Go language", author: "www.example.com"})
}
The output result is:
{Go 语言 www.example.com Go 语言教程 6495407}
{Go 语言 www.example.com Go 语言教程 6495407}
{Go 语言 www.example.com 0}
Accessing struct members
To access struct members, you need to use the dot (.) operator..Operator, in the format:
struct.member name
Struct type variables are defined using the struct keyword, as in the following example:
Example
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Declare Book1 as Books type */
var Book2 Books /* Declare Book2 as Books type */
/* Description of book 1 */
Book1.title = "Go language"
Book1.author = "www.example.com"
Book1.subject = "Go language tutorial"
Book1.book_id = 6495407
/* Description of book 2 */
Book2.title = Python Tutorial
Book2.author = "www.example.com"
Book2.subject = Python Language Tutorial
Book2.book_id = 6495700
/* Print Book1 information */
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)
/* Print Book2 information */
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.example.com Book 1 subject : Go 语言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.example.com Book 2 subject : Python 语言教程 Book 2 book_id : 6495700
Structs as function parameters
You can pass a struct type as a parameter to a function just like other data types, and access the struct variables in the manner of the above examples:
Example
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Declare Book1 as Books type */
var Book2 Books /* Declare Book2 as Books type */
/* Description of book 1 */
Book1.title = "Go language"
Book1.author = "www.example.com"
Book1.subject = "Go language tutorial"
Book1.book_id = 6495407
/* Description of book 2 */
Book2.title = Python Tutorial
Book2.author = "www.example.com"
Book2.subject = Python Language Tutorial
Book2.book_id = 6495700
/* Print Book1 information */
printBook(Book1)
/* Print Book2 information */
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.example.com Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.example.com Book subject : Python 语言教程 Book book_id : 6495700
struct pointer
You can define a pointer to a struct similarly to other pointer variables. The format is as follows:
var struct_pointer *Books
The pointer variable defined above can store the address of a struct variable. To view the address of a struct variable, you can place the & symbol before the struct variable:
struct_pointer = &Book1
Use the "." operator to access struct members via a struct pointer:
struct_pointer.title
Next, let's use a struct pointer to rewrite the above example, the code is as follows:
Example
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Declare Book1 as Books type */
var Book2 Books /* Declare Book2 as Books type */
/* Description of book 1 */
Book1.title = "Go language"
Book1.author = "www.example.com"
Book1.subject = "Go language tutorial"
Book1.book_id = 6495407
/* Description of book 2 */
Book2.title = Python Tutorial
Book2.author = "www.example.com"
Book2.subject = Python Language Tutorial
Book2.book_id = 6495700
/* Print Book1 information */
printBook(&Book1)
/* Print Book2 information */
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.example.com Book subject : Go 语言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.example.com Book subject : Python 语言教程 Book book_id : 6495700other extensions