Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mongo go驱动程序中使用接口类型作为模型? package code\u测试 进口( “编码/json” “fmt” “go.mongodb.org/mongo-driver/bson” “测试” ) 类型数据结构{ 输入int32`bson:“类型”` 伊瓦鲁·伊格特:“伊瓦鲁”` } func(m*数据)解组SALBSON(b[]字节)错误{ tempData:=&struct{ 输入int32`bson:“类型”` }{} 错误:=bson.Unmarshal(b,tempData) 如果错误!=零{ fmt.Println(“A”,错误) } 开关tempData.Type{ 案例100: reallyData:=&struct{ 输入int32`bson:“类型”` IValue*AGetter`bson:“IValue”` }{} err=bson.Unmarshal(b,reallyData) m、 Type=reallyData.Type m、 IValue=reallyData.IValue } 归零 } 类型IGetter接口{ Get()字符串 MarshallBSON()([]字节,错误) } 类型AGetter结构{ 数据字符串 } func(m*AGetter)Get()字符串{ 返回“AGETER”+m.DataA } func(m*AGetter)marshallbson()([]字节,错误){ t:=&struct{ 数据字符串 }{ DataA:m.DataA, } 返回bson.Marshal(t) //返回bson.Marshal(m) } func TestD(t*testing.t){ { 数据:=&data{ 类型:100, 伊瓦鲁:&艾格特{ DataA:“aaa”, }, } byteData,err:=bson.Marshal(数据) 如果错误!=零{ fmt.Println(错误) } var after=&数据{} err=bson.Unmarshal(byteData,after) fmt.Printf(“%s\n”,字符串化(后面)) } } func Stringify(o接口{})字符串{ //数据,err:=json.marshallindent(o,“,”) 数据,err:=json.Marshal(o) 如果错误!=零{ //xlog.Error(错误) 返回err.Error() } 返回字符串(数据) }_Mongodb_Go_Mongo Go - Fatal编程技术网

如何在mongo go驱动程序中使用接口类型作为模型? package code\u测试 进口( “编码/json” “fmt” “go.mongodb.org/mongo-driver/bson” “测试” ) 类型数据结构{ 输入int32`bson:“类型”` 伊瓦鲁·伊格特:“伊瓦鲁”` } func(m*数据)解组SALBSON(b[]字节)错误{ tempData:=&struct{ 输入int32`bson:“类型”` }{} 错误:=bson.Unmarshal(b,tempData) 如果错误!=零{ fmt.Println(“A”,错误) } 开关tempData.Type{ 案例100: reallyData:=&struct{ 输入int32`bson:“类型”` IValue*AGetter`bson:“IValue”` }{} err=bson.Unmarshal(b,reallyData) m、 Type=reallyData.Type m、 IValue=reallyData.IValue } 归零 } 类型IGetter接口{ Get()字符串 MarshallBSON()([]字节,错误) } 类型AGetter结构{ 数据字符串 } func(m*AGetter)Get()字符串{ 返回“AGETER”+m.DataA } func(m*AGetter)marshallbson()([]字节,错误){ t:=&struct{ 数据字符串 }{ DataA:m.DataA, } 返回bson.Marshal(t) //返回bson.Marshal(m) } func TestD(t*testing.t){ { 数据:=&data{ 类型:100, 伊瓦鲁:&艾格特{ DataA:“aaa”, }, } byteData,err:=bson.Marshal(数据) 如果错误!=零{ fmt.Println(错误) } var after=&数据{} err=bson.Unmarshal(byteData,after) fmt.Printf(“%s\n”,字符串化(后面)) } } func Stringify(o接口{})字符串{ //数据,err:=json.marshallindent(o,“,”) 数据,err:=json.Marshal(o) 如果错误!=零{ //xlog.Error(错误) 返回err.Error() } 返回字符串(数据) }

如何在mongo go驱动程序中使用接口类型作为模型? package code\u测试 进口( “编码/json” “fmt” “go.mongodb.org/mongo-driver/bson” “测试” ) 类型数据结构{ 输入int32`bson:“类型”` 伊瓦鲁·伊格特:“伊瓦鲁”` } func(m*数据)解组SALBSON(b[]字节)错误{ tempData:=&struct{ 输入int32`bson:“类型”` }{} 错误:=bson.Unmarshal(b,tempData) 如果错误!=零{ fmt.Println(“A”,错误) } 开关tempData.Type{ 案例100: reallyData:=&struct{ 输入int32`bson:“类型”` IValue*AGetter`bson:“IValue”` }{} err=bson.Unmarshal(b,reallyData) m、 Type=reallyData.Type m、 IValue=reallyData.IValue } 归零 } 类型IGetter接口{ Get()字符串 MarshallBSON()([]字节,错误) } 类型AGetter结构{ 数据字符串 } func(m*AGetter)Get()字符串{ 返回“AGETER”+m.DataA } func(m*AGetter)marshallbson()([]字节,错误){ t:=&struct{ 数据字符串 }{ DataA:m.DataA, } 返回bson.Marshal(t) //返回bson.Marshal(m) } func TestD(t*testing.t){ { 数据:=&data{ 类型:100, 伊瓦鲁:&艾格特{ DataA:“aaa”, }, } byteData,err:=bson.Marshal(数据) 如果错误!=零{ fmt.Println(错误) } var after=&数据{} err=bson.Unmarshal(byteData,after) fmt.Printf(“%s\n”,字符串化(后面)) } } func Stringify(o接口{})字符串{ //数据,err:=json.marshallindent(o,“,”) 数据,err:=json.Marshal(o) 如果错误!=零{ //xlog.Error(错误) 返回err.Error() } 返回字符串(数据) },mongodb,go,mongo-go,Mongodb,Go,Mongo Go,该代码工作成功 但我不想编写MarshalBSON函数。 就像在mgo driver中一样: 似乎只需要解组salbson功能。 但是当我在mongo go驱动程序中删除MarshallBSON时,我得到了一个错误“找不到用于code\u test.IGetter的编码器” 在找到一个好的解决方案之前,我用这个代码来解决这个问题 package mongo\u测试 进口( “fmt” mgoBson“github.com/globalsign/mgo/bson” “go.mongodb.org

该代码工作成功

但我不想编写MarshalBSON函数。

就像在mgo driver中一样:

似乎只需要解组salbson功能。

但是当我在mongo go驱动程序中删除MarshallBSON时,我得到了一个错误“找不到用于code\u test.IGetter的编码器”


在找到一个好的解决方案之前,我用这个代码来解决这个问题

package mongo\u测试
进口(
“fmt”
mgoBson“github.com/globalsign/mgo/bson”
“go.mongodb.org/mongo-driver/bson”
“测试”
“xkit”
)
类型数据结构{
输入int32`bson:“类型”`
伊瓦鲁·伊格特:“伊瓦鲁”`
}
func(m*数据)解组SALBSON(b[]字节)错误{
tempData:=&struct{
输入int32`bson:“类型”`
}{}
错误:=bson.Unmarshal(b,tempData)
如果错误!=零{
fmt.Println(“A”,错误)
}
开关tempData.Type{
案例100:
reallyData:=&struct{
输入int32`bson:“类型”`
IValue*AGetter`bson:“IValue”`
}{}
err=bson.Unmarshal(b,reallyData)
m、 Type=reallyData.Type
m、 IValue=reallyData.IValue
}
归零
}
func(m*数据)MarshalBSON()([]字节,错误){
返回mgoBson.Marshal(m)
}
类型IGetter接口{
Get()字符串
}
类型AGetter结构{
数据字符串
}
func(m*AGetter)Get()字符串{
返回“AGETER”+m.DataA
}
func TestD(t*testing.t){
{
数据:=&data{
类型:100,
伊瓦鲁:&艾格特{
DataA:“aaa”,
},
}
byteData,err:=bson.Marshal(数据)
如果错误!=零{
fmt.Println(错误)
}
var after=&数据{}
err=bson.Unmarshal(byteData,after)
fmt.Printf(“%s\n”,xkit.Stringify(后面))
}
}