目录
文章目录
- 目录
- 结构体
- 访问结构体成员
- 向函数传递结构体
- 结构体指针
结构体
Golang 中,结构体是由一系列具有相同类型或不同类型的数据构成的数据集合。
结构体的定义使用了 type 和 struct 关键字。
- struct 关键字:定义一个新的数据类型,结构体中有一个或多个成员。
- type 关键字:设定了结构体的名称。
结构体的格式如下:
type struct_variable_type struct {
member definition
member definition
...
member definition
}
一旦定义了结构体类型,它就能用于变量的声明,与声明基础数据类型的变量无异。
格式:
variable_name := structure_variable_type {value1, value2...valuen}
// or
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.runoob.com", "Go 语言教程", 6495407})
// 也可以使用 key => value 格式
fmt.Println(Books{title: "Go 语言", author: "www.runoob.com", subject: "Go 语言教程", book_id: 6495407})
// 忽略初始化的成员为 0 或 nil
fmt.Println(Books{title: "Go 语言", author: "www.runoob.com"})
}
访问结构体成员
使用 .
成员访问符,来访问结构体的成员。
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books
var Book2 Books
Book1.title = "Go 语言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 语言教程"
Book1.book_id = 6495407
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 语言教程"
Book2.book_id = 6495700
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)
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)
}
向函数传递结构体
你可以像其他数据类型一样将结构体类型变量作为参数传递给函数,结构体是作为实参时采用的是值传递。
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books
var Book2 Books
Book1.title = "Go 语言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 语言教程"
Book1.book_id = 6495407
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 语言教程"
Book2.book_id = 6495700
printBook(Book1)
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)
}
结构体指针
相对的,将结构体指针作为函数实参传入时,肯定是引用语义的。
声明结构体指针类型变量:
var struct_pointer *Books
struct_pointer = &Book1
Golang 中访问结构体指针类型变量的成员依然是使用成员访问符 .
,而不像 C 语言那样会有特殊的地址访问符 ->
。
struct_pointer.title
示例:
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books
var Book2 Books
Book1.title = "Go 语言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 语言教程"
Book1.book_id = 6495407
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 语言教程"
Book2.book_id = 6495700
printBook(&Book1)
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)
}