Go language structure


Release date:2023-09-16 Update date:2023-10-13 Editor:admin View counts:233

Label:

Go language structure

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

A structure is a collection of data that consists of a series of data of thesame type or different types.

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

  • Title

  • Author

  • Subject

  • ID

Define the structure

The structure definition needs to use the type and struct statement. struct statement defines a new data type with one or more members in the structure. 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,and the syntax format is as follows:

variable_name := structure_variable_type {value1, value2...valuen}
or
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() {
    // Create a new structure
    fmt.Println(Books{"Go language", "www.runoob.com", "Go Language Tutorial",
6495407})
    // You can also use the key=>value format
    fmt.Println(Books{title: "Go language", author: "www.runoob.com",
subject: "Go Language Tutorial", book_id: 6495407})
    // Ignored field is 0 or empty
   fmt.Println(Books{title: "Go language", author: "www.runoob.com"})
}

The output is as follows:

{Go Language www.runoob.com Go Language Tutorial 6495407}
{Go Language www.runoob.com Go Language Tutorial 6495407}
{Go Language www.runoob.com 0}

Access structure members

If you want to access structure members, you need to use a period . operator in the format:

Structure.Member name“

Structure type variables use the struct keyword definition. An example 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        /* Declare Book1 as a Book type */
   var Book2 Books        /* Declare Book2 as a Book type */
   /* book 1 describe */
   Book1.title = "Go Language"
   Book1.author = "www.runoob.com"
   Book1.subject = "Go Language Tutorial"
   Book1.book_id = 6495407
   /* book 2 describe */
   Book2.title = "Python Tutorial"
   Book2.author = "www.runoob.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 Language
Book 1 author : www.runoob.com
Book 1 subject : Go Language Tutorial
Book 1 book_id : 6495407
Book 2 title : Python Tutorial
Book 2 author : www.runoob.com
Book 2 subject : Python Language Tutorial
Book 2 book_id : 6495700

Structure as a function parameter

You can pass the structure type as an argument to the function like any other data type. And access the structure variables as shown 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 as a Book type */
   var Book2 Books        /* Declare Book2 as a Book type */
   /* book 1 describe */
   Book1.title = "Go language"
   Book1.author = "www.runoob.com"
   Book1.subject = "Go Language Tutorial"
   Book1.book_id = 6495407
   /* book 2 describe */
   Book2.title = "Python Tutorial"
   Book2.author = "www.runoob.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 Language
Book author : www.runoob.com
Book subject : Go Language Tutorial
Book book_id : 6495407
Book title : Python Tutorial
Book author : www.runoob.com
Book subject : Python Language Tutorial
Book book_id : 6495700

Structure pointer

You can define pointers to structures that are 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 address of the structure variable, you can set the & symbol is placed in front of the structural variable:

struct_pointer = &Book1

Use structure pointers to access structure members, using the "." operator:

struct_pointer.title

Next, let’s rewrite the above example with a structure pointer, 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 as a Book type */
   var Book2 Books        /* Declare Book2 as a Book type */
   /* book 1 describe */
   Book1.title = "Go language"
   Book1.author = "www.runoob.com"
   Book1.subject = "Go Language Tutorials"
   Book1.book_id = 6495407
   /* book 2 describe */
   Book2.title = "Python Tutorials"
   Book2.author = "www.runoob.com"
   Book2.subject = "Python Language Tutorials"
   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 Language
Book author : www.runoob.com
Book subject : Go Language Tutorial
Book book_id : 6495407
Book title : Python Tutorial
Book author : www.runoob.com
Book subject : Python Language Tutorial
Book book_id : 6495700

Powered by TorCMS (https://github.com/bukun/TorCMS).