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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM