Go数组可让您定义可容纳相同种类的变量,Structures结构是Go编程中用户定义的数据类型,它允许您组合不同类型的数据项。

假设您想跟踪图书馆中的书籍,您可能要跟踪每本书的以下属性-

无涯教程网

  • Title(标题)
  • Author(作者)
  • Subject(主题)
  • Book ID(图书ID)

定义结构

要定义Structures结构,必须使用 typestruct 语句。 struct语句定义一个新的数据类型,该程序具有多个成员,在我们的示例中,type语句将名称与struct绑定在一起。 struct语句的格式如下-

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

定义Structures类型后,即可使用以下语法将其用于声明该类型的变量。

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

访问结构

要访问Structures的任何成员,我们使用成员访问运算符(.),您可以使用 struct 关键字定义Structures类型的变量。以下示例说明了如何使用Structures-

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)
}

编译并执行上述代码后,将产生以下输出-

链接:https://www.learnfk.com/article-go/go_structures

来源:LearnFk无涯教程网

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);
}

编译并执行上述代码后,将产生以下输出-

链接:https://www.learnfk.com/article-go/go_structures

来源:LearnFk无涯教程网

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);
}

编译并执行上述代码后,将产生以下输出-

链接:https://www.learnfk.com/article-go/go_structures

来源:LearnFk无涯教程网

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

这一章你学到了什么?来做个笔记,好记忆不如烂笔头! 如果觉得对您有帮助,麻烦帮分享给您的朋友。

祝学习愉快! (如果觉得不正确,选中要修改的内容->右键->编辑)

点我分享笔记