struct結構如下:
package models import ( "github.com/robfig/revel" ) type Post struct { id int title string }
我在另一個包里面使用
package controllers import ( "blog/app/models" "fmt" "github.com/coopernurse/gorp" "github.com/robfig/revel" ) type Application struct { *revel.Controller Txn *gorp.Transaction } func (c Application) Index() revel.Result { post := &models.Post{1, "title"} fmt.Println(post) return c.Render() }
會出現如下錯誤:
implicit assignment of unexported field
原因是,struct定義的屬性是小寫開頭的,不是public的,這樣是不能跨包調用的!
正確的寫法應該是
type Post struct { Id int Title string }
屬性大寫開關
Have fun with golang!