package main import "fmt" type book struct { title string author string page int } func main() { var book1 book book1.author = "Adong" book1.page = 100 book1.title = "Story of Adong" fmt.Println(book1.author, book1.page, book1.title) }
//输出结果:
Adong 100 Story of Adong
package main import "fmt" type book struct { title string author string page int } func main() { var book1 book book1.author = "Adong" book1.page = 100 book1.title = "Story of Adong" print_book(book1) } func print_book(b book) { //这里函数接收到的参数是结构体 fmt.Println(b.author, b.page, b.title) }
结构体指针:
package main import "fmt" type book struct { title string author string page int } func main() { var book1 book book1.author = "Adong" book1.page = 100 book1.title = "Story of Adong" print_book(book1) var ptr *book = &book1 fmt.Println("book=", ptr.title) } func print_book(b book) { //这里函数接收到的参数是结构体 fmt.Println(b.author, b.page, b.title) }