Go语言结构体

Go数组允许定义一种可以保存多个相同类型的数据项的变量类型,但结构体是Go编程中可用的另一个用户定义的数据类型,它允许组合不同类型的数据项。

结构体用于表示记录,假设想在图书馆中跟踪图书。可能希望跟踪每本图书的以下属性:

  • 标题(Title)
  • 作者(Author)
  • 科目(Subject)
  • 图书编号(Book ID)

定义结构体

要定义结构,必须使用typestruct语句。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 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

以下是纠正/补充内容:

结构体就像是面向对象中的类一样,结构体中的成员就像是类中的成员一样。  提交时间:2019-09-02

上一篇:Go语言指针

下一篇:Go语言切片

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程