<p>I am pulling my hear over this problem. Unmarshal is simply not working, even though I copied it verbatim from the documentation.
config.json</p>
<pre><code>{
"testKey": "test_value"
}
</code></pre>
<p>main.go</p>
<pre><code>package main
import (
"fmt"
"log"
"io/ioutil"
"encoding/json"
)
type Configuration struct {
testKey string
}
func main() {
// Read config file
b, err := ioutil.ReadFile("config.json") // I checked b, it contains the config file contents
if err != nil {
log.Fatalf("Error reading config.json: %s", err)
}
configuration := Configuration{}
err = json.Unmarshal(b, &configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s", configuration.testKey) // Prints nothing, just empty
}
</code></pre>
<hr/>**评论:**<br/><br/>Justinsaccount: <pre><p>yeah, that's a fun one. From the docs:</p>
<p>To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. Unmarshal will only set exported fields of the struct. </p>
<p>you likely want something like</p>
<pre><code>type Configuration struct {
TestKey string `json:"test_key"`
}
</code></pre></pre>sjalfurstaralfur: <pre><p>Thanks, adding the <code>json</code> part made it work!</p>
<p>Also... this is weird but it only works the struct fields are CapitalizedLikeThis, not capitalizedLikeThis... is this some implicit rule of golang structs that I did not know about? Seems so weird.</p></pre>skidooer: <pre><blockquote>
<p>is this some implicit rule of golang structs that I did not know about?</p>
</blockquote>
<p>Not only structs, it is <a href="https://golang.org/ref/spec#Exported_identifiers" rel="nofollow">a general rule in Go</a>. <code>CapitalizedLikeThis</code> is exported (pubic), <code>capitalizedLikeThis</code> is unexported (private).</p>
<p>The <code>json</code> package doesn't have permission, if you will, to modify <code>capitalizedLikeThis</code>.</p></pre>Justinsaccount: <pre><p>no.. capitalizing the name is what made it work.</p></pre>sjalfurstaralfur: <pre><p>Ok, I did not know about this Go characteristic, TIL. Thanks for the help</p></pre>thepciet: <pre><p>Yup this is a standard gotcha everybody has to go through, but having JSON just work is worth it.</p></pre>Jherden: <pre><p>also, for future reference, you can pick brains on the #go-nuts IRC as well. getting answers are hit or miss, but sometimes it's worth potentially getting the quick feedback.</p></pre>gogroob: <pre><p>you can only Unmarshal to an exported struct key. Try
<code>
Configuration struct {
TestKey string
}
</code></p></pre>KyleBrandt: <pre><p>Note if you are working with existing JSON. You can pipe the text to go-json: <a href="https://github.com/ChimeraCoder/gojson" rel="nofollow">https://github.com/ChimeraCoder/gojson</a> , and it will generate a go struct for you, with the json struct tags. </p>
<p>Huge time saver.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传