package main
import(
"fmt"
)
type Book struct {
name string
id int
}
// 如果想要传指针用*Book,不传指针可以写func(book Book),或者func readBook(book Book)
// js的私有方法是在内部,go的在外部,(内)是告诉谁能调用,book.readBook
func (book Book) readBook () {
book.name = "lalala"
book.id = 123
fmt.Println(book)
}
// 上面的是方法,下面是函数,如果要传入指针,需要&来转,否则用上面的写法
func queryBook (book *Book) {
}
func main() {
book := Book{}
book.readBook()
fmt.Println(book.name)
queryBook(&book)
}
传送指针会改变book.name ,不传送指针则不改变
网友评论