[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