嗨,我用的是gorp和mysql。当插入struct时返回
reflect.Value.Interface:无法返回从未导出字段或方法获得的值
在gorp中,Panics if any interface in the list has not been registered with AddTable说,但是我添加了这个结构
这个问题总线结构有方法吗??我的程序和我的恐慌帮助我
type BUS struct {
Id int64 `db:"Idx"`
Created int64
Writer string `db:"Writer"`
WriterId int64
Title string `db:"Title"`
Content string `db:"Content"`
Want int64
status int64
}
func (b BUS) search(bf Board_find) []BUS {
var arr []BUS
query, query_map := bf.Prepare()
_, err := dbmap.Select(&arr, query, query_map)
if err != nil {
log.Print(err)
}
return arr
}
func (b* BUS) write() {
log.Print(reflect.TypeOf(b)) //just test code
err := dbmap.Insert(b)
check_err(err, "error in bus write")
}
func make_dbmap() *gorp.DbMap {
db, err := sql.Open("mysql", "tester:tester@tcp(127.0.0.1:3306)/TEST")
check_err(err, "db connection error")
log.Println("db connection Ok")
dialect := gorp.MySQLDialect{"InnoDB", "UTF8"}
dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
AddTable(dbmap, USER_DB{}, "USER")
table := AddTable(dbmap, BUS{}, "BUSBOARD")
table.ColMap("Writer").SetMaxSize(10)
table.ColMap("Title").SetMaxSize(25)
table.ColMap("Content").SetMaxSize(50)
log.Println("Add Table in gorp Ok")
return dbmap
}发布于 2015-07-09 16:22:12
这个错误;reflect.Value.Interface: cannot return value obtained from unexported field or method是最轻巧的,因为status int64未导出(至少我没有看到可能导致它的其他未导出字段)。修复与上套管Status一样简单。
在Go中,具有大小写名称的结构上的字段将被导出,而大小写名较低的字段则不会导出。未导出字段是语言与“私有”字段最接近的东西。在这种情况下,gorp或任何抛出的错误都反映了您的类型,找到了status字段并试图返回它,这是当您错误的时候,因为该字段是未导出的。
https://stackoverflow.com/questions/31322945
复制相似问题