English 中文(简体)
Go - 结构
  • 时间:2024-11-03

Go - 结构体



Go数组允许您定义可以容纳多个相同类型数据项的变量。Structure是Go编程中提供的另一种用户定义的数据类型,它允许您组合不同类型的数据项。

结构用于表示记录。假设你想跟踪图书馆里的书。您可能需要跟踪每本书的以下属性-

    Title

    Author

    Subject

    Book ID

在这种情况下,结构非常有用。

Defining a Structure

若要定义结构,必须使用类型和结构语句。struct语句定义了一个新的数据类型,其中包含程序的多个成员。type语句将一个名称与类型绑定,在我们的例子中,类型是struct。struct语句的格式如下−

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

一旦定义了结构类型,就可以使用以下语法来声明该类型的变量。

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

访问结构成员

要访问结构的任何成员,我们使用成员访问运算符(.)。成员访问运算符编码为结构变量名和我们希望访问的结构成员之间的句点。您可以使用struct关键字来定义结构类型的变量。以下示例解释了如何使用结构−


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 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Bilpng"
   Book2.author = "Zara Ap"
   Book2.subject = "Telecom Bilpng Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   fmt.Printf( "Book 1 title : %s
", Book1.title)
   fmt.Printf( "Book 1 author : %s
", Book1.author)
   fmt.Printf( "Book 1 subject : %s
", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d
", Book1.book_id)

   /* print Book2 info */
   fmt.Printf( "Book 2 title : %s
", Book2.title)
   fmt.Printf( "Book 2 author : %s
", Book2.author)
   fmt.Printf( "Book 2 subject : %s
", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d
", Book2.book_id)
}

When the above code is compiled and executed, it produces the following result −

Book 1 title      : Go Programming
Book 1 author     : Mahesh Kumar
Book 1 subject    : Go Programming Tutorial
Book 1 book_id    : 6495407
Book 2 title      : Telecom Bilpng
Book 2 author     : Zara Ap
Book 2 subject    : Telecom Bilpng Tutorial
Book 2 book_id    : 6495700

作为函数参数的结构

您可以像传递任何其他变量或指针一样,将结构作为函数参数进行传递。访问结构变量的方式与上述示例中的方式相同


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 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Bilpng"
   Book2.author = "Zara Ap"
   Book2.subject = "Telecom Bilpng Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   printBook(Book1)

   /* print Book2 info */
   printBook(Book2)
}
func printBook( book Books ) {
   fmt.Printf( "Book title : %s
", book.title);
   fmt.Printf( "Book author : %s
", book.author);
   fmt.Printf( "Book subject : %s
", book.subject);
   fmt.Printf( "Book book_id : %d
", book.book_id);
}

When the above code is compiled and executed, it produces the following result −

Book title     : Go Programming
Book author    : Mahesh Kumar
Book subject   : Go Programming Tutorial
Book book_id   : 6495407
Book title     : Telecom Bilpng
Book author    : Zara Ap
Book subject   : Telecom Bilpng Tutorial
Book book_id   : 6495700

指向结构的指针

您可以定义指向结构的指针,方法与定义指向任何其他变量的指针相同,如下所示−

var struct_pointer *Books

现在,您可以将结构变量的地址存储在上面定义的指针变量中。要查找结构变量的地址,请将&运算符放在结构名称之前,如下所示−

struct_pointer = &Book1;

要使用指向某个结构的指针访问该结构的成员,必须使用“.”运算符,如下所示−

struct_pointer.title;

Let us re-write the above example using structure pointer −


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 specification */
   Book1.title = "Go Programming"
   Book1.author = "Mahesh Kumar"
   Book1.subject = "Go Programming Tutorial"
   Book1.book_id = 6495407

   /* book 2 specification */
   Book2.title = "Telecom Bilpng"
   Book2.author = "Zara Ap"
   Book2.subject = "Telecom Bilpng Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   printBook(&Book1)

   /* print Book2 info */
   printBook(&Book2)
}
func printBook( book *Books ) {
   fmt.Printf( "Book title : %s
", book.title);
   fmt.Printf( "Book author : %s
", book.author);
   fmt.Printf( "Book subject : %s
", book.subject);
   fmt.Printf( "Book book_id : %d
", book.book_id);
}

When the above code is compiled and executed, it produces the following result −

Book title     : Go Programming
Book author    : Mahesh Kumar
Book subject   : Go Programming Tutorial
Book book_id   : 6495407
Book title     : Telecom Bilpng
Book author    : Zara Ap
Book subject   : Telecom Bilpng Tutorial
Book book_id   : 6495700