{"id":42891,"date":"2024-12-01T09:32:37","date_gmt":"2024-12-01T01:32:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42891\/"},"modified":"2024-12-01T09:32:37","modified_gmt":"2024-12-01T01:32:37","slug":"go-%e5%9c%a8%e8%bd%ac%e6%8d%a2%e4%b8%ba-json-%e4%b9%8b%e5%89%8d%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e6%b5%ae%e7%82%b9%e6%95%b0%e6%97%a0%e7%a9%b7%e5%a4%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42891\/","title":{"rendered":"Go \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Go \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927<\/span><\/p>\n<h1>Go \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 09:24:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u9047\u5230\u8fc7\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u6709\u4e00\u4e9b\u53ef\u80fd\u662f\u65e0\u7a77\u5927\/nan \u7684 float64 \u5b57\u6bb5\uff0c\u5e76\u4e14\u5c1d\u8bd5\u5c01\u9001\u81f3 json \u4f1a\u5bfc\u81f4\u6709\u5173\u4e0d\u652f\u6301 +inf \u7c7b\u578b\u7684\u9519\u8bef\u3002<\/p>\n<pre>type something interface {\n  id string `firestore:\"id\"`\n  numbera float64 `firestore:\"numbera\"`\n  numberb float64 `firestore:\"numberb\"`\n  numberc float64 `firestore:\"numberc\"`\n}<\/pre>\n<p>\u6b64\u7ed3\u6784\u6700\u521d\u662f\u901a\u8fc7\u53e6\u4e00\u4e2a\u5e93 (google firestore) \u586b\u5145\u7684\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u8981\u5927\u5f97\u591a\uff0c\u6709\u66f4\u591a\u7684\u6d6e\u70b9\u6570\u5b57\u6bb5\u3002<\/p>\n<p>\u6211\u60f3\u6211\u53ef\u4ee5\u4f7f\u7528\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u5faa\u73af\uff0c\u4f7f\u7528\u53cd\u5c04\u6765\u627e\u5230\u5b83\u4eec\uff0c\u5c3d\u7ba1\u6211\u60f3\u77e5\u9053\u662f\u5426\u6709\u66f4\u5e72\u51c0\u7684\u65b9\u6cd5\u6216\u66f4\u60ef\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>v := reflect.ValueOf(structVar)\ntypeOfS := v.Type()\nfor i := 0; i&lt; v.NumField(); i++ {\n  if typeOfS.Field(i).Type.Kind() == reflect.Float64 &amp;&amp; math.IsInf(v.Field(i).Interface().(float64), 1) {\n    \/\/ ... some logic I'll put here\n  }\n}<\/pre>\n<p>\u6211\u4e0d\u660e\u767d\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7f16\u7ec4\uff0c\u6240\u4ee5\u4e5f\u8bb8\u8fd9\u53ef\u4ee5\u662f\u5904\u7406 +inf \u7684\u9009\u9879\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>marshaler<\/code> \u63a5\u53e3\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u6765\u5b8c\u6210\u503c\u7684\u81ea\u5b9a\u4e49\u5904\u7406\u3002\u4f46\u662f\uff0c\u60a8\u7684 <code>something<\/code> \u7c7b\u578b\u683c\u5f0f\u9519\u8bef\u3002\u5b83\u88ab\u5b9a\u4e49\u4e3a <code>type something \u63a5\u53e3{}<\/code>\uff0c\u800c\u5b9e\u9645\u4e0a\u5e94\u8be5\u662f <code>type something struct<\/code>\uff1a<\/p>\n<pre>type something struct {\n    id      string    `firestore:\"id\"`\n    numbera jsonfloat `firestore:\"numbera\"`\n    numberb jsonfloat `firestore:\"numberb\"`\n    numberc jsonfloat `firestore:\"numberc\"`\n}\n\ntype jsonfloat float64\n\nfunc (j jsonfloat) marshaljson() ([]byte, error) {\n    v := float64(j)\n    if math.isinf(j, 0) {\n        \/\/ handle infinity, assign desired value to v\n        \/\/ or say +\/- indicates infinity\n        s := \"+\"\n        if math.isinf(v, -1) {\n            s = \"-\"\n        }\n        return []byte(s), nil\n    }\n    return json.marshal(v) \/\/ marshal result as standard float64\n}\n\nfunc (j *jsonfloat) unsmarshaljson(v []byte) error {\n    if s := string(v); s == \"+\" || s == \"-\" {\n        \/\/ if +\/- indiciates infinity\n        if s == \"+\" {\n            *j = jsonfloat(math.inf(1))\n            return nil\n        }\n        *j = jsonfloat(math.inf(-1))\n        return nil\n    }\n    \/\/ just a regular float value\n    var fv float64\n    if err := json.unmarshal(v, &amp;fv); err != nil {\\\n        return err\n    }\n    *j = jsonfloat(fv)\n    return nil\n}<\/pre>\n<p>\u5e94\u8be5\u53ef\u4ee5\u4e86<\/p>\n<p>\u6211\u521b\u5efa\u4e86  \u6765\u652f\u6301 nan\u3001+inf \u548c -inf\u3002<\/p>\n<pre>type T struct {\n    N  float64\n    IP float64\n    IN float64\n}\n\nfunc TestMarshalNaNAndInf(t *testing.T) {\n    s := T{\n        N:  math.NaN(),\n        IP: math.Inf(1),\n        IN: math.Inf(-1),\n    }\n    got, err := Marshal(s)\n    if err != nil {\n        t.Errorf(\"Marshal() error: %v\", err)\n    }\n    want := `{\"N\":NaN,\"IP\":+Inf,\"IN\":-Inf}`\n    if string(got) != want {\n        t.Errorf(\"Marshal() = %s, want %s\", got, want)\n    }\n}\n\nfunc TestUnmarshalNaNAndInf(t *testing.T) {\n    data := []byte(`{\"N\":NaN,\"IP\":+Inf,\"IN\":-Inf}`)\n    var s T\n    err := Unmarshal(data, &amp;s)\n    if err != nil {\n        t.Fatalf(\"Unmarshal: %v\", err)\n    }\n    if !math.IsNaN(s.N) || !math.IsInf(s.IP, 1) || !math.IsInf(s.IN, -1)     {\n        t.Fatalf(\"after Unmarshal, s.N=%f, s.IP=%f, s.IN=%f, want NaN, +Inf, -Inf\", s.N, s.IP, s.IN)\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGo \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Go \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927 Go \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927 \u6765\u6e90\uff1astackoverflow 2024-04-27 09:24:36 0\u6d4f\u89c8 \u6536\u85cf Golang\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aGo \u5728\u8f6c\u6362\u4e3a JSON \u4e4b\u524d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u65e0\u7a77\u5927\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u9047\u5230\u8fc7\u4e00\u79cd\u60c5\u51b5\uff0c\u6211\u6709\u4e00\u4e9b\u53ef\u80fd\u662f\u65e0\u7a77\u5927\/nan \u7684 float64 \u5b57\u6bb5\uff0c\u5e76\u4e14\u5c1d\u8bd5\u5c01\u9001\u81f3 json \u4f1a\u5bfc\u81f4\u6709\u5173\u4e0d\u652f\u6301 +inf \u7c7b\u578b\u7684\u9519\u8bef\u3002 type something interface { id string `firestore:&#8221;id&#8221;` numbera float64 `firestore:&#8221;numbera&#8221;` numberb float64 `firestore:&#8221;numberb&#8221;` numberc float64 `firestore:&#8221;numberc&#8221;` } \u6b64\u7ed3\u6784\u6700\u521d\u662f\u901a\u8fc7\u53e6\u4e00\u4e2a\u5e93 (google firestore) \u586b\u5145\u7684\u3002 \u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u8981\u5927\u5f97\u591a\uff0c\u6709\u66f4\u591a\u7684\u6d6e\u70b9\u6570\u5b57\u6bb5\u3002 \u6211\u60f3\u6211\u53ef\u4ee5\u4f7f\u7528\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u5faa\u73af\uff0c\u4f7f\u7528\u53cd\u5c04\u6765\u627e\u5230\u5b83\u4eec\uff0c\u5c3d\u7ba1\u6211\u60f3\u77e5\u9053\u662f\u5426\u6709\u66f4\u5e72\u51c0\u7684\u65b9\u6cd5\u6216\u66f4\u60ef\u7528\u7684\u65b9\u6cd5\u3002 v [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42891","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42891"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42891\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}