Help with encoding and decoding Json Data?

blov · · 428 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello all, I am trying to build a simple chat app to learn more about golang and am getting tripped up by json. When I try to decode I get no output and the &#34;in&#34; variable is empty. Here is my code so far for my client and server.</p> <p>client:</p> <pre><code>func main() { out := Message{name: &#34;Jacob&#34;, text: &#34;hey whats up&#34;} conn, err := net.Dial(&#34;tcp&#34;, &#34;127.0.0.1:6666&#34;) if err != nil { panic(err) } err := json.NewEncoder(conn).Encode(&amp;out) if err != nil { panic(err) } } </code></pre> <p>server:</p> <pre><code>func main() { in := new(Message) server, _ := net.Listen(&#34;tcp&#34;, &#34;127.0.0.1:6666&#34;) conn, _ := server.Accept() err := json.NewDecoder(conn).Decode(&amp;in) if err != nil { panic(err) } fmt.Println(in.name+&#34;:&#34;, in.text) } </code></pre> <p>But when I run the two, my only output is &#34;:&#34;. I know it has to be a dumb mistake, so any help or advice is appreciated!</p> <hr/>**评论:**<br/><br/>Perelandric: <pre><p>Go can&#39;t encode/decode private fields.</p> <hr/> <p><em>EDIT:</em> Here&#39;s a working example with exported fields and field tags: <a href="https://play.golang.org/p/y-DmJ_iTPj" rel="nofollow">https://play.golang.org/p/y-DmJ_iTPj</a></p></pre>Eniac17: <pre><p>THANK YOU. Oh my I&#39;ve been freaking out over this for two hours and forgot how go handled private fields. Oh the wonders of learning a new language </p></pre>Killing_Spark: <pre><p>So your example works because you defined message explicitly as a type with those two fields? </p> <p>Sorry if this question is dumb I just started reading up on go :) </p></pre>Perelandric: <pre><p>No, the <code>Message</code> type was just left out of the question. It worked because I made the fields of <code>Message</code> public, meaning they can be accessed by code outside of the package in which it was defined.</p> <p>Fields are made public by starting their name with a capital letter. So in the original code, it would have looked like this:</p> <pre><code>type Message struct { name string text string } </code></pre> <p>That defines a struct with two, private fields. Mine capitalizes those field names:</p> <pre><code>type Message struct { Name string Text string } </code></pre> <p>So now they&#39;re readable/writable by any code that gets a value of that type.</p> <p>This is necessary because the <code>json</code> package <em>(or more accurately, the <code>reflect</code> package)</em> can only work with fields that it can access.</p></pre>Killing_Spark: <pre><p>Ah I see. Thanks for the thorough answer :) </p></pre>danredux: <pre><p>code looks good but i think maybe you&#39;re using too many indirections? maybe?</p> <pre><code>in := new(Message) // in is now of type *Message Decode(&amp;in) // now you&#39;re passing **Message </code></pre> <p>same goes for your encode..</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

428 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传