Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型。
结构体是由一系列具有相同类型或不同类型的数据构成的数据集合。
结构体表示一项记录,比如保存图书馆的书籍记录,每本书有以下属性:
Title :标题
Author : 作者
Subject:学科
ID:书籍ID
结构体定义需要使用 type 和 struct 语句。struct 语句定义一个新的数据类型,结构体中有一个或多个成员。type 语句设定了结构体的名称。结构体的格式如下:
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