Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Go language structure


May 11, 2021 Go


Table of contents


Go language structure

Arrays in the Go language can store the same type of data, but in a structure we can define different data types for different items.

A structure is a collection of data that consists of data of the same type or type.

A structure represents a record, such as keeping a library's book record, each with the following attributes:

  • Title: Title
  • Author: Author
  • Subject: Subject
  • ID: Book ID

Define the structure

Structure definitions require type and struct statements. T he struct statement defines a new data type, and the structure has one or more members. T he 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 the declaration of variables in the following syntax format:

variable_name := structure_variable_type {value1, value2...valuen}

Access structure members

If you want to access a member of a structure, you need to use the dot (.) operator in the format: "Structure. Member name."

Structure type variables are defined using the struct keyword, 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.w3cschool.cn"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3cschool.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 above instances perform the run as follows:

Book 1 title : Go 语言
Book 1 author : www.w3cschool.cn
Book 1 subject : Go 语言教程
Book 1 book_id : 6495407
Book 2 title : Python 教程
Book 2 author : www.w3cschool.cn
Book 2 subject : Python 语言教程
Book 2 book_id : 6495700

The structure is used as a function argument

You can pass structure types to functions as arguments, just like any other data type. And access the structure variables in the same way as the above examples:

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.w3cschool.cn"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3cschool.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 above instances perform the run as follows:

Book title : Go 语言
Book author : www.w3cschool.cn
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3cschool.cn
Book subject : Python 语言教程
Book book_id : 6495700

Structure pointer

You can define pointers to structures similar to other pointer variables in the following format:

var struct_pointer *Books

The pointer variables defined above can store the addresses of the structure variables. Looking at 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 members of the structure, using the "." operator:

struct_pointer.title;

Let's rewrite the above example with a structure pointer, and the code looks like this:

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.w3cschool.cn"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3cschool.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 above instances perform the run as follows:

Book title : Go 语言
Book author : www.w3cschool.cn
Book subject : Go 语言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.w3cschool.cn
Book subject : Python 语言教程
Book book_id : 6495700