Go Language Structs
In Go, arrays can store data of the same type, but in a struct, we can define different data types for different items.
A struct is a collection of data composed of fields with the same or different types.
A struct 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
Defining a Struct
Struct definition requires the use of type
and struct
statements. The struct
statement defines a new data type, with one or more members in the struct. 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 the struct type is defined, it can be used for variable declaration, with the following syntax:
variable_name := struct_variable_type {value1, value2...valuen}
or
variable_name := struct_variable_type { key1: value1, key2: value2..., keyn: valuen}
Here is an example:
Example
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.tutorialpro.org", "Go Language Tutorial", 6495407})
// Can also use key => value format
fmt.Println(Books{title: "Go Language", author: "www.tutorialpro.org", subject: "Go Language Tutorial", book_id: 6495407})
// Omitted fields are 0 or empty
fmt.Println(Books{title: "Go Language", author: "www.tutorialpro.org"})
}
The output is:
{Go Language www.tutorialpro.org Go Language Tutorial 6495407}
{Go Language www.tutorialpro.org Go Language Tutorial 6495407}
{Go Language www.tutorialpro.org 0}
Accessing Struct Members
To access struct members, you need to use the dot .
operator, with the following format:
struct.member_name
Struct type variables are defined using the struct
keyword. Here is an example:
Example
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.tutorialpro.org"
Book1.subject = "Go Language Tutorial"
Book1.book_id = 6495407
/* Book 2 description */
Book2.title = "Python Tutorial"
Book2.author = "www.tutorialpro.org"
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 above example execution result is:
Book 1 title: Go Language Book 1 author: www.tutorialpro.org Book 1 subject: Go Language Tutorial Book 1 book_id: 6495407 Book 2 title: Python Tutorial Book 2 author: www.tutorialpro.org Book 2 subject: Python Language Tutorial Book 2 book_id: 6495700
---
## Structure as Function Argument
You can pass a structure type as an argument to a function just like any other data type, and access the structure variables in the same way as in 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 /* Declare Book1 of type Books / var Book2 Books / Declare Book2 of type Books */
/* book 1 specification */ Book1.title = "Go Language" Book1.author = "www.tutorialpro.org" Book1.subject = "Go Language Tutorial" Book1.book_id = 6495407
/* book 2 specification */ Book2.title = "Python Tutorial" Book2.author = "www.tutorialpro.org" Book2.subject = "Python Language Tutorial" Book2.book_id = 6495700
/* print Book1 info */ printBook(Book1)
/* print Book2 info */ 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 result is:
Book title: Go Language Book author: www.tutorialpro.org Book subject: Go Language Tutorial Book book_id: 6495407 Book title: Python Tutorial Book author: www.tutorialpro.org Book subject: Python Language Tutorial Book book_id: 6495700
---
## Structure Pointer
You can define pointers to structures just like you define pointers to any other variable, as follows:
var struct_pointer *Books
The pointer variable defined above can store the address of a structure variable. To get the address of the structure variable, place the `&` symbol in front of the structure variable:
struct_pointer = &Book1
To access the members of the structure using a pointer to that structure, use the `.` operator:
struct_pointer.title
Let's rewrite the above example using structure pointers, as follows:
## Example
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 Books / var Book2 Books / Declare Book2 of type Books */
/* book 1 specification */ Book1.title = "Go Language" Book1.author = "www.tutorialpro.org" Book1.subject = "Go Language Tutorial" Book1.book_id = 6495407
/* book 2 specification */ Book2.title = "Python Tutorial" Book2.author = "www.tutorialpro.org" Book2.subject = "Python Language Tutorial" Book2.book_id = 6495700
/* print Book1 info */ printBook(&Book1)
/* print Book2 info */ 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 result is:
Book title: Go Language Book author: www.tutorialpro.org Book subject: Go Language Tutorial Book book_id: 6495407 Book title: Python Tutorial Book author: www.tutorialpro.org Book subject: Python Language Tutorial Book book_id: 6495700
/* book 1 description */ Book1.title = "Go Language" Book1.author = "www.tutorialpro.org" Book1.subject = "Go Language Tutorial" Book1.book_id = 6495407
/* book 2 description */ Book2.title = "Python Tutorial" Book2.author = "www.tutorialpro.org" 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 above example execution result is:
Book title : Go Language Book author : www.tutorialpro.org Book subject : Go Language Tutorial Book book_id : 6495407 Book title : Python Tutorial Book author : www.tutorialpro.org Book subject : Python Language Tutorial Book book_id : 6495700 ```