发表于 2018-9-20 10:48:26

[golang] implicit assignment of unexported field

  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!


页: [1]
查看完整版本: [golang] implicit assignment of unexported field