<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 == "" || config.RefreshToken == "") {
initOAuth()
} else {
fmt.Println("Nope")
}
}
func initOAuth() {
config := readConfig()
srv := startServer()
auth.SetAuthInfo(config.ClientID, config.ClientSecret)
url := auth.AuthURL(state)
fmt.Println("Please log in to Spotify by visiting the following page in your browser:", url)
client := <-ch
// my code panics if comment out following line!
fmt.Println(srv)
if err := srv.Shutdown(nil); err != nil {
log.Fatal("Couldn't shutdown the server: ", err)
}
user, err := client.CurrentUser()
if err != nil {
log.Fatal(err)
}
fmt.Println("You are logged in as:", user.ID)
}
func startServer() *http.Server {
srv := &http.Server{Addr: ":8080"}
http.HandleFunc("/callback", completeAuth)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Got request for:", r.URL.String())
})
go func() {
if err := srv.ListenAndServe(); err != nil {
fmt.Println("Error with the server", err)
}
}()
return srv
}
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s\n", st, state)
}
// use the token to get an authenticated client
client := auth.NewClient(tok)
fmt.Fprintf(w, "Login Completed!")
ch <- &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't remove that print statement, it works fine without any issues :|</p>
<hr/>**评论:**<br/><br/>peterbourgon: <pre><p>You're reading from and writing to the channel <code>ch</code>, but it'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传