位置:首页 > 高级语言 > Go语言在线教程 > Go语言结构

Go语言结构

Go数组允许定义类型的变量,可容纳同类的多个数据项,但结构在Go编程,它允许结合不同种类的数据项,可使用其他用户定义的数据类型。

结构是用来代表一个记录,假设要跟踪图书馆的图书。可能要跟踪有关每本书以下属性:

  • Title(书名)

  • Author(作者)

  • Subject(科目)

  • Book ID(编号)

定义结构

定义一个结构,必须使用type和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 Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing Tutorial"
   Book2.book_id = 6495700
 
   /* print Book1 info */
   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 info */
   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)
}

当上述代码被编译和执行时,它产生了以下结果:

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 Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing 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 Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing 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);
}

当上述代码被编译和执行时,它产生了以下结果:

Book title : Go Programming
Book author : Mahesh Kumar
Book subject : Go Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

指针结构

可以非常相似定义指针结构的方式,为您定义指向任何其他变量,如下所示:

var struct_pointer *Books

现在,你可以存储结构变量的地址在上面定义的指针变量。为了找到一个结构变量的地址,使用&运算符结构在名字前,如下所示:

struct_pointer = &Book1;

要访问结构的使用指针成员的结构,您必须使用“.”运算符,如下:

struct_pointer.title;

让我们使用结构指针重新写上面的例子,希望这可以帮助您理解概念:

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 Billing"
   Book2.author = "Zara Ali"
   Book2.subject = "Telecom Billing 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);
}

当上述代码被编译和执行时,它产生了以下结果:

Book title : Go Programming
Book author : Mahesh Kumar
Book subject : Go Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700