Go基础 专题
专题目录
您的位置:go > Go基础 专题 > Go语言结构体
Go语言结构体
作者:--    发布时间:2019-11-20

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
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册