What am I doing wrong? (channels and goroutine)

polaris · · 429 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am new to channels and goroutines. So something weird happening and I am trying to figure out. In below script there is a print statement and if I remove that, my code panics. If not, it runs fine :| </p> <p>And following is my script: </p> <pre><code>func main() { config := readConfig() if (config.AccessToken == &#34;&#34; || config.RefreshToken == &#34;&#34;) { initOAuth() } else { fmt.Println(&#34;Nope&#34;) } } func initOAuth() { config := readConfig() srv := startServer() auth.SetAuthInfo(config.ClientID, config.ClientSecret) url := auth.AuthURL(state) fmt.Println(&#34;Please log in to Spotify by visiting the following page in your browser:&#34;, url) client := &lt;-ch // my code panics if comment out following line! fmt.Println(srv) if err := srv.Shutdown(nil); err != nil { log.Fatal(&#34;Couldn&#39;t shutdown the server: &#34;, err) } user, err := client.CurrentUser() if err != nil { log.Fatal(err) } fmt.Println(&#34;You are logged in as:&#34;, user.ID) } func startServer() *http.Server { srv := &amp;http.Server{Addr: &#34;:8080&#34;} http.HandleFunc(&#34;/callback&#34;, completeAuth) http.HandleFunc(&#34;/&#34;, func(w http.ResponseWriter, r *http.Request) { log.Println(&#34;Got request for:&#34;, r.URL.String()) }) go func() { if err := srv.ListenAndServe(); err != nil { fmt.Println(&#34;Error with the server&#34;, err) } }() return srv } func completeAuth(w http.ResponseWriter, r *http.Request) { tok, err := auth.Token(state, r) if err != nil { http.Error(w, &#34;Couldn&#39;t get token&#34;, http.StatusForbidden) log.Fatal(err) } if st := r.FormValue(&#34;state&#34;); st != state { http.NotFound(w, r) log.Fatalf(&#34;State mismatch: %s != %s\n&#34;, st, state) } // use the token to get an authenticated client client := auth.NewClient(tok) fmt.Fprintf(w, &#34;Login Completed!&#34;) ch &lt;- &amp;client } </code></pre> <p>Here is what it is supposed to do:</p> <ol> <li>Check the config and if those keys are empty, then start an HTTP Server (which can handle OAuth callback) and ask user to do OAuth</li> <li>Handle the OAuth callback in <code>completeAuth</code></li> <li>Once OAuth done, pass the value to channel</li> <li>Once there is a value in Channel, kill the started HTTP Server and move on</li> </ol> <p>Here is what the error says:</p> <pre><code>Error with the server http: Server closed panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x1208e92] goroutine 1 [running]: net/http.(*Server).Shutdown(0xc4200a02c0, 0x0, 0x0, 0x0, 0x0) /usr/local/opt/go/libexec/src/net/http/server.go:2466 +0x152 main.initOAuth() /Users/avi/.go/src/github.com/avinassh/virtu/main.go:39 +0x262 main.main() /Users/avi/.go/src/github.com/avinassh/virtu/main.go:25 +0x61 </code></pre> <p>However if I don&#39;t remove that print statement, it works fine without any issues :|</p> <hr/>**评论:**<br/><br/>peterbourgon: <pre><p>You&#39;re reading from and writing to the channel <code>ch</code>, but it&#39;s not defined anywhere. Please link to your <em>complete</em> code somewhere?</p></pre>avinassh: <pre><p>Sure, here it is - <a href="https://github.com/avinassh/virtu/blob/b085cd33062402a630b15c1b25c005ae88b41d16/main.go" rel="nofollow">link</a></p></pre>

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

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