Latest web development tutorials

Go language structure

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

Structure is composed of a series of data sets of the same type or of different types of data constituted.

Structure represents a record, such as saving a record library of books, each book has the following properties:

  • Title: Title
  • Author: Author
  • Subject: Subject
  • ID: Books ID

Definition of the structure

Structure is defined and requires the use of type struct statement. struct will define a new data type, the structure has one or more members. type name statement sets the structure. Format structure is as follows:

type struct_variable_type struct {
   member definition;
   member definition;
   ...
   member definition;
}

Once you have defined the type of structure, it can be used to declare variables, syntax is as follows:

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

To a structure

If you want to access a structure member, use the dot operator, the format (.):. "A structure member name."

Type variable structure using the struct keyword definitions, examples are 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.w3big.com"
   Book1.subject = "Go 语言教程"
   Book1.book_id = 6495407

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   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)
}

Examples of the implementation of the above operating results as follows:

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

The structure as a function parameter

You can like other data types to the type of structure passed to the function as a parameter. And by way of example above to access the structure variable:

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

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   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);
}

Examples of the implementation of the above operating results as follows:

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

Structure pointer

You can define a pointer pointing to a structure similar to other pointer variable, the format is as follows:

var struct_pointer *Books

Defined above can address memory pointer variable structure variables. See structure variable address, you can place an ampersand in front of the structure variables:

struct_pointer = &Book1;

Using a pointer to a structure member of a structure, use the operator. "":

struct_pointer.title;

Let's use the structure pointer rewrite the above example, the code below:

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

   /* book 2 描述 */
   Book2.title = "Python 教程"
   Book2.author = "www.w3big.com"
   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);
}

Examples of the implementation of the above operating results as follows:

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