获取错误:结构字面量中的未知字段 [英] Getting error: unknown field in struct literal
本文介绍了获取错误:结构字面量中的未知字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
type NullInt64 struct {
sql.NullInt64
}
$ b $ func ToNullInt64(s string)NullInt64 {
i,err:= strconv.Atoi(s)
return NullInt64 {Int64:int64(i),Valid:err == nil}
}
但是我得到这个错误:
.. \sql\sql.go:27:结构字面量中未知的NullInt64字段'Int64'
.. \sql\sql .go:27:未知的NullInt64字段在结构体字段中'Valid'
解决方案
要初始化嵌入式sql.NullInt64,你必须写:
NullInt64 {sql.NullInt64 {Int64:int64( i),Valid:err == nil}}
或者,如果您的NullInt64结构包含其他字段你不想显式初始化,你可以通过使用它的类型来访问嵌入式字段:
NullInt64 {NullInt64:sql。 NullInt64 {Int64:int64(i ),Valid:err == nil}}
I am trying to run this code:
type NullInt64 struct {
sql.NullInt64
}
func ToNullInt64(s string) NullInt64 {
i, err := strconv.Atoi(s)
return NullInt64{Int64: int64(i), Valid: err == nil}
}
but I get this error:
..\sql\sql.go:27: unknown NullInt64 field 'Int64' in struct literal
..\sql\sql.go:27: unknown NullInt64 field 'Valid' in struct literal
解决方案
To initialize the embedded sql.NullInt64, you have to write:
NullInt64{sql.NullInt64{Int64: int64(i), Valid: err == nil}}
or, if your NullInt64 struct contains other fields which you don't want to initialize explicitly, you can access the embedded field by using its type:
NullInt64{NullInt64: sql.NullInt64{Int64: int64(i), Valid: err == nil}}
这篇关于获取错误:结构字面量中的未知字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文