Golang 中国
`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 = "jim"
    book1.subject = "com"
    book2.title = "Python"
    book2.author = "al"
    book2.subject = "cm"

    printBook(&book1)
    printBook(&book2)
}

func printBook(book_ptr *Books) {

    fmt.Println(*book_ptr.title)
    // fmt.Printf("书名是%s", *book_ptr.title)
    // fmt.Printf("作者是%s", *book_ptr.author)
    // fmt.Printf("主题是%s", *book_ptr.subject)
    // fmt.Println()

定义结构体 然后作为指针参数传入函数后 这时的*book_ptr 不应该是 实际的结构体吗

为什么不能使用*book_ptr.title 获取它的title??

报错: .\demo.go:33:14: invalid indirect of book_ptr.title (type string)

2 回复
aaron
#1 aaron • 2018-06-12 14:56
  • 注意运算优先级
    fmt.Println((*book_ptr).title)
zhao6810
#2 zhao6810 • 2018-06-15 09:19

book := *book_ptr
book.title

需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。