go - 在 Go 中解组到结构时的奇怪行为
问题描述
我正在开发一种可以实施的工具,以简化创建简单 CRUD 操作/端点的过程。由于我的端点不知道他们将接收什么样的结构,我创建了一个用户可以实现的接口,并返回一个空对象来填充。
type ItemFactory interface {
GenerateEmptyItem() interface{}
}
用户会实现类似的东西:
type Test struct {
TestString string `json:"testString"`
TestInt int `json:"testInt"`
TestBool bool `json:"testBool"`
}
func (t Test) GenerateEmptyItem() interface{} {
return Test{}
}
当 Test 对象被创建时,它的类型是“Test”,即使函数返回了一个 interface{}。但是,一旦我尝试将一些相同格式的 json 解组到其中,它就会剥离它的类型,并变成“map [string]interface {}”类型。
item := h.ItemFactory.GenerateEmptyItem()
//Prints "Test"
fmt.Printf("%T\n", item)
fmt.Println(reflect.TypeOf(item))
err := ConvertRequestBodyIntoObject(r, &item)
if err != nil {...}
//Prints "map[string]interface {}"
fmt.Printf("%T\n", item)
解组项目的函数:
func ConvertRequestBodyIntoObject(request *http.Request, object interface{}) error {
body, err := ioutil.ReadAll(request.Body)
if err != nil {
return err
}
// Unmarshal body into request object
err = json.Unmarshal(body, object)
if err != nil {
return err
}
return nil
}
关于为什么会发生这种情况或我如何解决它的任何建议?
谢谢
解决方案
您的问题缺少显示此行为的示例,因此我只是猜测这就是正在发生的事情。
func Generate() interface{} {
return Test{}
}
func GeneratePointer() interface{} {
return &Test{}
}
func main() {
vi := Generate()
json.Unmarshal([]byte(`{}`), &vi)
fmt.Printf("Generate Type: %T\n", vi)
vp := GeneratePointer()
json.Unmarshal([]byte(`{}`), vp)
fmt.Printf("GenerateP Type: %T\n", vp)
}
哪个输出:
Generate Type: map[string]interface {}
GenerateP Type: *main.Test
我建议您返回一个指针,GenerateEmptyItem()
而不是示例中演示的实际结构值GenerateP()
。
推荐阅读
- sql - Oracle SQL 在字符串的数字部分之后选择字符
- python - 使用 networkx (Python) 创建和执行任务工作流
- java - 如何知道 com.sun.jdi.ObjectReference 是否是枚举?
- flutter - 为不同的字段值重用 Datepicker
- python - 从字典列表中提取信息
- mathematical-optimization - 我不明白为什么 x 没有价值
- javascript - 如何创建将在 html 和 js 中显示隐藏消息的密码
- javascript - hexo博客中括号不能正确显示
- c# - 如何关闭 Azure 函数中的 NLog 日志记录?
- r - R每天的行数