package main
import "fmt"
type Books struct { //左类型,右结构体
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* 声明 Book1 为 Books 类型 */
// book1的表述
Book1.title = "Go语言"
Book1.author = "shawn"
Book1.subject = "fight"
Book1.book_id = 123
/* 打印 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)
// 调用函数打印 Book1 信息
printBook(Book1)
// 结构体指针
var ptr *Books
ptr = &Book1
fmt.Println(ptr.author)
printBook1(&Book1) //调用结构体函数
}
// 结构体作为函数参数
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)
}
// 指针结构体函数
func printBook1(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)
}