- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我看来, reflect.ValueOf(&x).Elem()
等于 reflect.ValueOf(x)
因为 .Elem()
是获取reflect.Value
包含的指针的实际值。代码来了,json.Marshal
的编码结果是不同的:
func generateRequest(input string, flag bool) interface{} {
val := Node {
Cmd: "Netware",
Name: input,
}
if flag {
return &val
} else {
return val
}
}
func main() {
request1 := generateRequest("123", true)
request2 := generateRequest("123", false)
request1Val := reflect.ValueOf(request1).Elem()
fmt.Println(request1Val, request2)
json1, err := json.Marshal(request1Val)
checkErr(err)
json2, err := json.Marshal(request2)
checkErr(err)
fmt.Println(json1, string(json1))
fmt.Println(json2, string(json2))
fmt.Println(reflect.DeepEqual(json1, json2))
}
下面是输出:
{Netware 123} {Netware 123}
[123 34 102 108 97 103 34 58 52 48 57 125] {"flag":409}
[123 34 99 109 100 34 58 34 78 101 116 119 97 114 101 34 44 34 110 97 109 101 34 58 34 49 50 51 34 125] {"cmd":"Netware","name":"123"}
false
我想知道为什么它们不同,以及如何修改我的代码以使request1
的编码结果与request2
相同。
最佳答案
抱歉,这完全是错误的。反射和 JSON 编码不能并存。
json2, err := json.Marshal(request2)
部分是合理的:request2
是一个 Node
(包裹在 接口(interface){}
,这个事实在这里并不有趣)。因此,在其上调用 json.Marshal 将编码一个 Node
,这会导致 {"cmd":"Netware","name":"123"}
,这就是您想要的期待。
现在对于 json1, err := json.Marshal(request1Val)
:Go 是静态 类型化,而你的 request1Val
的类型为reflect.Value,它是Go 中完整的普通类型,如string
或type myFoo struct {whatever}
。如果将这种类型的内容传递给 json.Marshal,您将获得 reflect.Value
的 JSON 序列化。不幸的是,这种序列化在任何方面都是完全无用的,因为它与 Reflect.Value 中封装的值没有任何共同之处。将 Reflect.Values 视为包含您的请求的不透明容器1。不幸的是,它是不透明的,序列化不会神奇地揭示它隐藏的内容。
如果您想从reflect.Value转到它实际保存的内容,请使用它的Interface()
方法来“解开”容器并取回您在reflect.Value中包装的内容。
您的问题与reflect.ValueOf(&x).Elem()或reflect.Value(x)的不同之处无关。您的问题源于将reflect.Value传递给json.Marshal,无论reflect.Value实际包含什么,它都永远不会工作。
关于go - go中 `reflect.ValueOf(&x).Elem`和 `reflect.ValueOf(x)`有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49292854/
有没有办法像这样输出一个数组: (elem,elem,elem) 例如,如果数组是[2, 3, 4],它会打印: (2,3,4) 最佳答案 puts "(#{array.join ','})" 这是一
我正在使用 g++ -g 编译我的代码,但我在标题中收到了错误消息。 这个错误与我做的一个函数有关,它的签名是: void addHead( Elem *&start , Elem *newStart
我使用的代码如 $('.elem',elem)、$('.elem',elem).tabs()。 $(".elem") 用于选择具有该类的元素。 但是逗号后面的是什么?它有什么用? 最佳答案 $('.e
你可能看过我之前关于 jQuery 升级的主题。所以最后我们将jQUery从1.4.2升级到1.6.1,但我们遇到了以下问题: elem is undefinedif ( elem.nodeName
typedef struct Element { struct Element *next; void *data; } Element; 在 pop 函数中,(!(elem = *s
我今天想知道 javascript 函数。我知道 jQuery 是一个 javascript 库,可以在带有点的元素上调用函数。 javascript 有时会做同样的事情(例如:.toFixed())
这个问题在这里已经有了答案: val() vs. text() for textarea (2 个答案) 关闭 5 年前。 这很奇怪。显然,我可以同时使用 .val() 和 .text() 来操作文
这个问题已经有答案了: Which way to test if an element is checked is better? .is(':checked') or .prop('checked'
所以,基本上我想检测用户何时将鼠标悬停在一个元素上(不同的 div 元素,不是父元素或同级元素),并且当发生悬停时,添加 :hover到我的另一个 div 元素。我的其他 div 元素状态的 :hov
Type Driven Development with Idris 一书介绍了这个练习: Define a possible method that fits the signature: two
在下面的代码中,两个选项似乎都分配了相同的资源 func Allocate(v interface{}) error { rv := reflect.ValueOf(v) if rv.
我有以下内容: elem :: Eq a => a -> [a] -> Bool elem _ [] = False elem x (y:ys) = x == y || elem x ys 我如何证明
每当我在本地系统中运行脚本时,游标都能正常工作,当我在docker中运行时,我获取了一个错误,所以任何人都会告诉我哪里出了问题,或者这个问题是不是包端的问题。。当我在当地跑的时候,我无头:假,当时无头
我有外部 RSS 提要填充以下重复出现的类 elements 。 {teaserImage} {teaserImage} {teaserImage} 我想简单地获取 :first 实例,该实例也是来自
使用 python 的 openpyxl 加载 xlsm 文件,然后在将一些数据添加到特定工作表中的特定 7 个单元格后保存/关闭它时,我收到警告。问题是我收到了一个“FutureWarning”,我
问题是将列表元素的连续副本打包到子列表中。 我不明白elem在这里使用单个元素, 例如, pack [1,1,2,2,3,4] 然后x将为 1 和 (head (pack xs))将是 1。 怎么能:
$(elem) 的用途是什么?我什么时候使用它? 我的 javascript 函数有问题,我有 3 个表单,这 3 个表单有时共享相同的字段类,因此当我尝试对其中一个表单进行验证时,即使它已正确填写,
我有一个相当简单的问题,我通常可以自己调试,但我现在似乎遇到了很多问题。 我正在创建一个链表数据结构,我做了两个函数,一个返回前面的 Elem,一个返回最后一个 Elem。问题是编译器说 Elem 没
我开始使用 ES6 粗箭头函数符号,我非常喜欢它。但是我对它的上下文有点困惑。据我所知,关键字 this inside fat arrow function 指的是函数当前运行的上下文。我想做一些简单
Go语言程序中对指针获取反射对象时,可以通过 reflect.Elem() 方法获取这个指针指向的元素类型。这个获取过程被称为取元素,等效于对指针类型变量做了一个 *操作,代码如下: packag
我是一名优秀的程序员,十分优秀!