vit*_*ter 4 json pointers interface go unmarshalling
当使用interface{}
函数参数类型时,当给定非指针类型并使用json.Unmarshal
它时,我在 Go 中遇到了一个错误。
因为一段代码值一千字,所以举个例子:
package main
import (
"encoding/json"
"fmt"
)
func test(i interface{}) {
j := []byte(`{ "foo": "bar" }`)
fmt.Printf("%T\n", i)
fmt.Printf("%T\n", &i)
json.Unmarshal(j, &i)
fmt.Printf("%T\n", i)
}
type Test struct {
Foo string
}
func main() {
test(Test{})
}
Run Code Online (Sandbox Code Playgroud)
哪些输出:
main.Test
*interface {}
map[string]interface {}
Run Code Online (Sandbox Code Playgroud)
json.Unmarshal
将我的结构变成map[string]interface{}
oO ...
Little readings 后来解释了其中的一些,它interface{}
本身就是一种类型,而不是某种无类型的容器,它解释了*interface{}
,以及json.Unmarshal
无法获取初始类型的事实,并返回了一个map[string]interface{}
..
从Unmarshal
文档:
要将 JSON 解组为接口值,Unmarshal 将其中之一存储在接口值中:[...]
如果我像这样传递一个指向测试函数的指针,它会起作用:
func test(i interface{}) {
j := []byte(`{ "foo": "bar" }`)
fmt.Printf("%T\n", i)
fmt.Printf("%T\n", &i)
json.Unmarshal(j, i)
fmt.Printf("%T\n", i)
fmt.Println(i)
}
func main() {
test(&Test{})
}
Run Code Online (Sandbox Code Playgroud)
哪些输出:
*main.Test
*interface {}
*main.Test
&{bar}
Run Code Online (Sandbox Code Playgroud)
很酷,数据已解组,现在在第二个片段中,我删除了&
调用Unmarshal
. 因为我有一个*Test
in i
,没有用。
所以在所有逻辑中,如果我在调用它时放回&
to应该再次混淆's 类型。但不是。i
Unmarshal
i
如果我运行:
func test(i interface{}) {
j := []byte(`{ "foo": "bar" }`)
fmt.Printf("%T\n", i)
fmt.Printf("%T\n", &i)
json.Unmarshal(j, &i)
fmt.Printf("%T\n", i)
fmt.Println(i)
}
func main() {
test(&Test{})
}
Run Code Online (Sandbox Code Playgroud)
那么它仍然有效:
*main.Test
*interface {}
*main.Test
&{bar}
Run Code Online (Sandbox Code Playgroud)
现在我没有谷歌搜索查询了。
interface{}
是任何值和任何类型的包装器。接口示意性地包装了(value; type)
一对、具体值及其类型。更多细节:反射定律#接口的表示。
json.Unmarshal()
已经采用 type 的值interface{}
:
func Unmarshal(data []byte, v interface{}) error
Run Code Online (Sandbox Code Playgroud)
因此,如果您已经有了一个interface{}
值(函数的i interface{}
参数test()
),请不要尝试获取其地址,只需按原样传递即可。
另请注意,对于任何要修改存储在 中的值的包interface{}
,您都需要传递一个指向它的指针。所以应该i
是一个指针。所以正确的场景是传递*Test
到test()
,然后在内部test()
传递i
到json.Unmarshal()
(不获取它的地址)。
当i
contains*Test
并且您通过 时&i
,它将起作用,因为json
包将简单地取消引用*interface{}
指针,并找到一个interface{}
值,该*Test
值包装了一个值。它是一个指针,所以一切都很好:将 JSON 对象解组为指向的Test
值。
当i
containsTest
并且您通过 时&i
,与上面相同:*interface{}
被取消引用,因此它会找到interface{}
包含非指针的an :Test
。由于json
包不能解组为非指针值,它必须创建一个新值。由于传递给json.Unmarshal()
函数的值是 type *interface{}
,它告诉json
包将数据解组为 type 的值interface{}
。这意味着json
包可以自由选择要使用的类型。默认情况下,json
包将 JSON 对象解组为map[string]interface{}
值,因此这就是创建和使用的内容(并最终放入您传递的指针所指向的值中:)&i
。
总而言之,避免使用指向接口的指针。而是将指针“放入”接口(接口值应该包装指针)。当您已经有一个interface{}
持有指针时,只需将其传递即可。
归档时间: |
|
查看次数: |
708 次 |
最近记录: |