YouTip LogoYouTip

Go Structures

# Go Structures In Go, arrays can store data of the same type, but with structures, we can define different data types for different items. A structure is a collection of data items of the same or different types. A structure represents a record, for example, storing a library book record, where each book has the following attributes: * Title: The title * Author: The author * Subject: The subject * ID: The book ID * * * ## Defining a Structure To define a structure, you need to use the `type` and `struct` statements. The `struct` statement defines a new data type with one or more members. The `type` statement sets the name of the structure. The format of a structure is as follows: ```go 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: ```go variable_name := structure_variable_type {value1, value2...valuen} or ```go variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen } Here is an example: ## Example ```go package main 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..com", "Go LanguageTutorial", 6495407}) // Can also use key => value format fmt.Println(Books{title: "Go Language", author: "www..com", subject: "Go LanguageTutorial", book_id: 6495407}) // Ignored fields are 0 or empty fmt.Println(Books{title: "Go Language", author: "www..com"}) } The output is: {Go Language www..com Go LanguageTutorial 6495407} {Go Language www..com Go LanguageTutorial 6495407} {Go Language www..com 0} * * * ## Accessing Structure Members To access structure members, you use the dot `.` operator. The format is: ```go StructureVariable.MemberName A structure type variable is defined using the `struct` keyword. Here is an example: ## Example ```go package main import "fmt" type Books struct { title string author string subject string book_id int } func main() { var Book1 Books /* Declare Book1 as type Books */ var Book2 Books /* Declare Book2 as type Books */ /* book 1 description */ Book1.title = "Go Language" Book1.author = "www..com" Book1.subject = "Go LanguageTutorial" Book1.book_id = 6495407 /* book 2 description */ Book2.title = "Python Tutorial" Book2.author = "www..com" Book2.subject = "Python LanguageTutorial" Book2.book_id = 6495700 /* print Book1 info */ fmt.Printf("Book 1 title : %sn", Book1.title) fmt.Printf("Book 1 author : %sn", Book1.author) fmt.Printf("Book 1 subject : %sn", Book1.subject) fmt.Printf("Book 1 book_id : %dn", Book1.book_id) /* print Book2 info */ fmt.Printf("Book 2 title : %sn", Book2.title) fmt.Printf("Book 2 author : %sn", Book2.author) fmt.Printf("Book 2 subject : %sn", Book2.subject) fmt.Printf("Book 2 book_id : %dn", Book2.book_id) } The output of the above example is: Book 1 title : Go Language Book 1 author : www..com Book 1 subject : Go LanguageTutorial Book 1 book_id : 6495407 Book 2 title : Python Tutorial Book 2 author : www..com Book 2 subject : Python LanguageTutorial Book 2 book_id : 6495700 * * * ## Structure as Function Parameters You can pass a structure type as a parameter to a function, just like any other data type. You can access the structure variable in the same way as in the example above: ## Example ```go package main import "fmt" type Books struct { title string author string subject string book_id int } func main() { var Book1 Books /* Declare Book1 as type Books */ var Book2 Books /* Declare Book2 as type Books */ /* book 1 description */ Book1.title = "Go Language" Book1.author = "www..com" Book1.subject = "Go LanguageTutorial" Book1.book_id = 6495407 /* book 2 description */ Book2.title = "Python Tutorial" Book2.author = "www..com" Book2.subject = "Python LanguageTutorial" Book2.book_id = 6495700 /* print Book1 info */ printBook(Book1) /* print Book2 info */ printBook(Book2) } func printBook( book Books ) { fmt.Printf("Book title : %sn", book.title) fmt.Printf("Book author : %sn", book.author) fmt.Printf("Book subject : %sn", book.subject) fmt.Printf("Book book_id : %dn", book.book_id) } The output of the above example is: Book title : Go Language Book author : www..com Book subject : Go LanguageTutorial Book book_id : 6495407 Book title : Python Tutorial Book author : www..com Book subject : Python LanguageTutorial Book book_id : 6495700 * * * ## Structure Pointers You can define a pointer to a structure, similar to other pointer variables, as follows: ```go var struct_pointer *Books The pointer variable defined above can store the address of a structure variable. To view the address of a structure variable, place the `&` symbol before the structure variable: ```go struct_pointer = &Book1 To access structure members using a structure pointer, use the `.` operator: ```go struct_pointer.title Next, let's rewrite the above example using a structure pointer. The code is as follows: ## Example ```go package main import "fmt" type Books struct { title string author string subject string book_id int } func main() { var Book1 Books /* Declare Book1 as type Books */ var Book2 Books /* Declare Book2 as type Books */ /* book 1 description */ Book1.title = "Go Language" Book1.author = "www..com" Book1.subject = "Go LanguageTutorial" Book1.book_id = 6495407 /* book 2 description */ Book2.title = "Python Tutorial" Book2.author = "www..com" Book2.subject = "Python LanguageTutorial" Book2.book_id = 6495700 /* print Book1 info */ printBook(&Book1) /* print Book2 info */ printBook(&Book2) } func printBook( book *Books ) { fmt.Printf("Book title : %sn", book.title) fmt.Printf("Book author : %sn", book.author) fmt.Printf("Book subject : %sn", book.subject) fmt.Printf("Book book_id : %dn", book.book_id) } The output of the above example is: Book title : Go Language Book author : www..com Book subject : Go LanguageTutorial Book book_id : 6495407 Book title : Python Tutorial Book author : www..com Book subject : Python LanguageTutorial Book book_id : 6495700 [](#)(#) (#)[](#) [Volcengine Coding Plan supports Doubao, GLM, DeepSeek, Kimi, MiniMax and other mainstream large models, officially supplied, stable and reliable. Configuration Guide Β₯9.9/month Subscribe Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E) ## 7 Notes Write a Note 1. #0 Xinghai yan***anbao12@163.com [](#)142 Structures are passed by value as parameters: ```go package main import "fmt" type Books struct { title string author string subject string book_id int } func changeBook(book Books) { book.title = "book1_change" } func main() { var book1 Books book1.title = "book1" book1.author = "zuozhe" book1.book_id = 1 changeBook(book1) fmt.Println(book1) } The result is: {book1 zuozhe 1} If you want to change the content of the structure data inside the function, you need to pass a pointer: ```go package main import "fmt" type Books struct { title string author string subject string book_id int } func changeBook(book *Books) { book.title = "book1_change" } func main() { var book1 Books book1.title = "book1" book1.author = "zuozhe" book1.book_id = 1 changeBook(&book1) fmt.Println(book1) } The result is: {book1_change zuozhe 1} (javascript:;)Xinghai yan***anbao12@163.com 7 years ago (2019-02-25) 2. #0 Ng Li ngl***163.com [](#)51 `struct` is similar to a class in Java, and you can define member variables within a `struct`. To access member variables, there are two ways: * 1. Access via `struct_variable.member_variable`. * 2. Access via `struct_pointer->member_variable`. There is no need to use getter and setter methods to control access. ```go type Rect struct { // Define a rectangle class x, y float64 // The type only contains attributes, no methods width, height float64 } func (r *Rect) Area() float64 { // Bind the Area method to the Rect type. *Rect is a pointer reference, which can modify the value of the passed parameter. return r.width * r.height // The method belongs to the type, not to a specific object. Declare an object of that type to call the type's methods. } (javascript:;) Ng Li ngl***163.com 7 years ago (2019-05-08) 3. #0 MissLi luy***.w@yahoo.com (http://xn--dkr57f11hieo1se8fsl9arp7b2ss/) [](#)8 Using a pointer to change the value of a structure: ```go package main import ( "fmt" "strconv" ) type Books struct { title string author string subject string book_id int } func printBook(book Books) { /* Print function, no return value, pass in a structure */ /* Structure is only used as a temporary parameter */ fmt.Printf("Book title: %sn", book.title) fmt.Printf("Book author: %sn", book.author) fmt.Printf("Book subject: %sn", book.subject) fmt.Printf("Book id: %dn", book.book_id) } func changeBook(book *Books, new_info_type string, new_info string) { if new_info_type == "title" { book.title = new_info } else { if new_info_type == "author" { book.author = new_info } } } func main() { var book1 Books book1.title = "book1" book1.author = "zuozhe" book1.book_id = 1 printBook(book1) changeBook(&book1, "title", "book1_change") printBook(book1) } The result is: Book title: book1 Book author: zuozhe Book subject: Book id: 1 Book title: book1_change Book author: zuozhe Book subject: Book id: 1 (javascript:;)MissLi luy***.w@yahoo.com 7 years ago (2019-05-08)
← Go Passing Pointers To FunctioGo Array Of Pointers β†’