[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!

posted @ 2013-04-09 18:29  DavidHHuan  阅读(5534)  评论(0编辑  收藏  举报