Go 語言結構體
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} 或 variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}
實例如下:
實例
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// 創建一個新的結構體
fmt.Println(Books{"Go 語言", "www.xuhuhu.com", "Go 語言教學", 6495407})
// 也可以使用 key => value 格式
fmt.Println(Books{title: "Go 語言", author: "www.xuhuhu.com", subject: "Go 語言教學", book_id: 6495407})
// 忽略的字段為 0 或 空
fmt.Println(Books{title: "Go 語言", author: "www.xuhuhu.com"})
}
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// 創建一個新的結構體
fmt.Println(Books{"Go 語言", "www.xuhuhu.com", "Go 語言教學", 6495407})
// 也可以使用 key => value 格式
fmt.Println(Books{title: "Go 語言", author: "www.xuhuhu.com", subject: "Go 語言教學", book_id: 6495407})
// 忽略的字段為 0 或 空
fmt.Println(Books{title: "Go 語言", author: "www.xuhuhu.com"})
}
輸出結果為:
{Go 語言 www.xuhuhu.com Go 語言教學 6495407} {Go 語言 www.xuhuhu.com Go 語言教學 6495407} {Go 語言 www.xuhuhu.com 0}
訪問結構體成員
如果要訪問結構體成員,需要使用點號 . 操作符,格式為:
結構體.成員名"
結構體類型變數使用 struct 關鍵字定義,實例如下:
實例
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* 聲明 Book1 為 Books 類型 */
var Book2 Books /* 聲明 Book2 為 Books 類型 */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
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)
/* 列印 Book2 資訊 */
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)
}
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* 聲明 Book1 為 Books 類型 */
var Book2 Books /* 聲明 Book2 為 Books 類型 */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
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)
/* 列印 Book2 資訊 */
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 語言 Book 1 author : www.xuhuhu.com Book 1 subject : Go 語言教學 Book 1 book_id : 6495407 Book 2 title : Python 教學 Book 2 author : www.xuhuhu.com Book 2 subject : Python 語言教學 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 /* 聲明 Book1 為 Books 類型 */
var Book2 Books /* 聲明 Book2 為 Books 類型 */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
printBook(Book1)
/* 列印 Book2 資訊 */
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)
}
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* 聲明 Book1 為 Books 類型 */
var Book2 Books /* 聲明 Book2 為 Books 類型 */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
printBook(Book1)
/* 列印 Book2 資訊 */
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 語言 Book author : www.xuhuhu.com Book subject : Go 語言教學 Book book_id : 6495407 Book title : Python 教學 Book author : www.xuhuhu.com Book subject : Python 語言教學 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 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
printBook(&Book1)
/* 列印 Book2 資訊 */
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)
}
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 描述 */
Book1.title = "Go 語言"
Book1.author = "www.xuhuhu.com"
Book1.subject = "Go 語言教學"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教學"
Book2.author = "www.xuhuhu.com"
Book2.subject = "Python 語言教學"
Book2.book_id = 6495700
/* 列印 Book1 資訊 */
printBook(&Book1)
/* 列印 Book2 資訊 */
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 語言 Book author : www.xuhuhu.com Book subject : Go 語言教學 Book book_id : 6495407 Book title : Python 教學 Book author : www.xuhuhu.com Book subject : Python 語言教學 Book book_id : 6495700