I am new to golang. I need help understanding the go client.

blov · · 484 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, <a href="/r/golang" rel="nofollow">r/golang</a>. I am new to goland and got a challenge to understand this golang client. After that, I need to implement the server to make this client actually work. Here is link to the code: <a href="https://play.golang.org/p/pCXTaCaQsn" rel="nofollow">https://play.golang.org/p/pCXTaCaQsn</a></p> <p>Basically, I have finished implementing the server for the client after 3 days trying to understand the client. The response from the server would be: {&#34;Init&#34;:0,&#34;Add&#34;:0,&#34;Normalize&#34;:0,&#34;Dump&#34;:9000} for GET request and &#34;HELLO&#34; for POST request.</p> <p>The problem is, the client exits at line 165. Is there anyway to make it run all the way to the end ? Am I missing something ?</p> <hr/>**评论:**<br/><br/>ChristophBerger: <pre><p>Line 156 logs an err value. What does the message say? </p> <p>A tip for troubleshooting: There are some packages like <a href="https://go-proverbs.github.io/" rel="nofollow">spew</a> or <a href="https://go-proverbs.github.io/" rel="nofollow">q</a> that can help inspecting the actual values of variables during execution. Just put some spew.Dump(...) or q.Q(...) calls into the functions that return with an error (CallWithoutResult and Call) and you can see if there is something wrong with the data that these functions process.</p> <p>This should get you quickly to the root cause.</p></pre>aragon0510: <pre><p>Line 156: I believe if the GetIndex() returns it right, the err should be nil. Otherwise, it will log whatever err returned from GetIndex()</p></pre>ChristophBerger: <pre><p>Apologies! I made a typo, I meant line 165 of course. (The one you mentioned as the line that errors out.)</p> <p>Let&#39;s try breaking this down:</p> <p>Line 165 executes if <code>CallWithoutResult</code> returns a non-nill error.</p> <p><code>CallWithoutResult</code> receives an URL string and an <code>Atom</code> (that implements <code>Serializer</code>). It returns the error value it gets from <code>Call</code>.</p> <p><code>Call</code>takes over the parameters from <code>CallWithoutResult</code>and returns a non-nil error in two cases:</p> <ol> <li><code>http.Post</code> returns an error, or</li> <li><code>http.Post</code> returns status code 300 or greater.</li> </ol> <p><code>http.Post</code> receives an URL as <code>string</code>, a fixed string &#34;application/octet-stream&#34;, and the result of <code>Union(args)</code> of type io.Reader. <code>args</code> is a slice of type <code>Serializer</code> (an interface). </p> <p><code>Union</code> receives a slice of <code>Serializer</code>s, creates a pipe, and spawns a goroutine that iterates over the slice and writes the serialized values to the pipe.</p> <p>So the code ultimately enters line 165 if <code>http.Post</code> errors out for one of the two reasons mentioned above. No other parts of the scenario generate new errors. </p> <p>-&gt; Find out which of the two reasons apply. The error message should tell whether <code>http.Post</code> returned an error or rather a status code &gt;= 300. </p> <p>-&gt; Inspect the data that http.Post receives. Check the values of <code>url</code> and of <code>args</code> just before <code>http.Post</code> is invoked. These may give a hint about why <code>http.Post</code> errors out. </p></pre>ChristophBerger: <pre><p>Just noticed something strange:</p> <p>This</p> <pre><code> if CallWithoutResult(url, ops.Dump) == nil { os.Exit(1) } </code></pre> <p>will exit if CallWithoutResult returns NO error. It will continue to line 164 if there is an error.</p> <p>Is this the intended behavior? Should this perhaps rather read <code>if ... != nil</code>?</p></pre>aragon0510: <pre><p>I thought about that last night and tried to comment out the block in line 164 (supposed the http.post returned no error).</p> <p>After I commented out the block in line 164, the code ran to line 197 =&gt; break and exit at 208.</p> <p>I am thinking the json I send from the server is not right json.</p></pre>ChristophBerger: <pre><p>Agreed, most probably the error is caused by wrong data.</p></pre>aragon0510: <pre><blockquote> <p>{&#34;Init&#34;:0,&#34;Add&#34;:0,&#34;Normalize&#34;:0,&#34;Dump&#34;:9000}</p> </blockquote> <p>this is what i came up. Mostly because Index has Atom type (which is int type) members</p></pre>ChristophBerger: <pre><p>Is this the JSON that GetIndex receives?</p></pre>aragon0510: <pre><p>Yes. I made a small server to send that json through the url</p></pre>ChristophBerger: <pre><p>Ok, then the first CallWithoutResult gets ops.Dump, which is 9000, and the second one (that fails) gets ops.Init, which is 0. Looks like there is a problem when passing this 0 to http.Post (via Union()). I cannot tell from the code where the problem is; I guess it&#39;s on the server&#39;s end.</p></pre>

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

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