没有不值得去解决的问题,也没有不值得去学习的技术!

Runtime Error: Invalid Memory Address or Nil Pointer Dereference When Using the URL Shortener API

Check the terminal output, which reports the error: runtime error: invalid memory address or nil pointer dereference.

1. Reference URL: https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.11.md . Test by browsing the page at http://localhost:8080/ . Enter http://www.destandaard.be in Shorten this, and click Give me the short URL. It keeps loading until it times out. See Figure 1.

Reference URL: https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.11.md . Test by browsing the page at http://localhost:8080/ . Enter http://www.destandaard.be in Shorten this, and click Give me the short URL. It keeps loading until it times out.
Figure 1

2. Check the terminal output, which reports the error: runtime error: invalid memory address or nil pointer dereference. See Figure 2.

Check the terminal output, which reports the error: runtime error: invalid memory address or nil pointer dereference.
Figure 2
Plaintext

PS E:\wwwroot\go\the-way-to-go\package> go run urlshortener.go
2020/09/07 19:52:23 http: panic serving 127.0.0.1:55417: runtime error: invalid memory address or nil pointer dereference
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc000282000)
        c:/go/src/net/http/server.go:1767 +0x140
panic(0x957240, 0xe59f20)
        c:/go/src/runtime/panic.go:679 +0x1c0
main.short(0xac7dc0, 0xc0002ac1c0, 0xc00029a100)
        E:/wwwroot/go/the-way-to-go/package/urlshortener.go:42 +0x18d
net/http.HandlerFunc.ServeHTTP(0xa2e470, 0xac7dc0, 0xc0002ac1c0, 0xc00029a100)
        c:/go/src/net/http/server.go:2007 +0x4b
net/http.(*ServeMux).ServeHTTP(0xe6f740, 0xac7dc0, 0xc0002ac1c0, 0xc00029a100)
        c:/go/src/net/http/server.go:2387 +0x1c4
net/http.serverHandler.ServeHTTP(0xc000264000, 0xac7dc0, 0xc0002ac1c0, 0xc00029a100)
        c:/go/src/net/http/server.go:2802 +0xab
net/http.(*conn).serve(0xc000282000, 0xac8b00, 0xc00025c080)
        c:/go/src/net/http/server.go:1890 +0x87c
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:2927 +0x395
2020/09/07 19:52:29 http: panic serving 127.0.0.1:55418: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc0002b4000)
        c:/go/src/net/http/server.go:1767 +0x140
panic(0x957240, 0xe59f20)
        c:/go/src/runtime/panic.go:679 +0x1c0
main.short(0xac7dc0, 0xc00030c000, 0xc0002b0100)
        E:/wwwroot/go/the-way-to-go/package/urlshortener.go:42 +0x18d
net/http.HandlerFunc.ServeHTTP(0xa2e470, 0xac7dc0, 0xc00030c000, 0xc0002b0100)
        c:/go/src/net/http/server.go:2007 +0x4b
net/http.(*ServeMux).ServeHTTP(0xe6f740, 0xac7dc0, 0xc00030c000, 0xc0002b0100)
        c:/go/src/net/http/server.go:2387 +0x1c4
net/http.serverHandler.ServeHTTP(0xc000264000, 0xac7dc0, 0xc00030c000, 0xc0002b0100)
        c:/go/src/net/http/server.go:2802 +0xab
net/http.(*conn).serve(0xc0002b4000, 0xac8b00, 0xc000266080)
        c:/go/src/net/http/server.go:1890 +0x87c
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:2927 +0x395
2020/09/07 19:52:40 http: panic serving 127.0.0.1:55438: runtime error: invalid memory address or nil pointer dereference
goroutine 14 [running]:
net/http.(*conn).serve.func1(0xc00011a820)
        c:/go/src/net/http/server.go:1767 +0x140
panic(0x957240, 0xe59f20)
        c:/go/src/runtime/panic.go:679 +0x1c0
main.short(0xac7dc0, 0xc0002640e0, 0xc000244200)
        E:/wwwroot/go/the-way-to-go/package/urlshortener.go:42 +0x18d
net/http.HandlerFunc.ServeHTTP(0xa2e470, 0xac7dc0, 0xc0002640e0, 0xc000244200)
        c:/go/src/net/http/server.go:2007 +0x4b
net/http.(*ServeMux).ServeHTTP(0xe6f740, 0xac7dc0, 0xc0002640e0, 0xc000244200)
        c:/go/src/net/http/server.go:2387 +0x1c4
net/http.serverHandler.ServeHTTP(0xc000264000, 0xac7dc0, 0xc0002640e0, 0xc000244200)
        c:/go/src/net/http/server.go:2802 +0xab
net/http.(*conn).serve(0xc00011a820, 0xac8b00, 0xc00004d280)
        c:/go/src/net/http/server.go:1890 +0x87c
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:2927 +0x395
exit status 2
PS E:\wwwroot\go\the-way-to-go\package>

3. Review the Go file, urlshortener.go. For code simplicity, we did not check the returned error status, but this check must be performed in a real production environment application. Open the URL: http://goo.gl , and find that the URL Shortener API stopped being supported as of March 30, 2019. See Figure 3.

Review the Go file, urlshortener.go. For code simplicity, we did not check the returned error status, but this check must be performed in a real production environment application. Open the URL: http://goo.gl , and find that the URL Shortener API stopped being supported as of March 30, 2019.
Figure 3
Go

package main

import (
	 "fmt"
	 "net/http"
	 "text/template"

	 "google.golang.org/api/urlshortener/v1"
)
func main() {
	 http.HandleFunc("/", root)
	 http.HandleFunc("/short", short)
	 http.HandleFunc("/long", long)

	 http.ListenAndServe("localhost:8080", nil)
}
// the template used to show the forms and the results web page to the user
var rootHtmlTmpl = template.Must(template.New("rootHtml").Parse(`

URL SHORTENER
{{if .}}{{.}}{{end}}

Shorten this: 




Expand this: http://goo.gl/



`))
func root(w http.ResponseWriter, r *http.Request) {
	rootHtmlTmpl.Execute(w, nil)
}
func short(w http.ResponseWriter, r *http.Request) {
	 longUrl := r.FormValue("longUrl")
	 urlshortenerSvc, _ := urlshortener.New(http.DefaultClient)
	 url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl:
	 longUrl,}).Do()
	 rootHtmlTmpl.Execute(w, fmt.Sprintf("Shortened version of %s is : %s",
	 longUrl, url.Id))
}

func long(w http.ResponseWriter, r *http.Request) {
	 shortUrl := "http://goo.gl/" + r.FormValue("shortUrl")
	 urlshortenerSvc, _ := urlshortener.New(http.DefaultClient)
	 url, err := urlshortenerSvc.Url.Get(shortUrl).Do()
	 if err != nil {
		 fmt.Println("error: %v", err)
		 return

	 }
	 rootHtmlTmpl.Execute(w, fmt.Sprintf("Longer version of %s is : %s",
	 shortUrl, url.LongUrl))
}

4. For developers who want to migrate to FDL, please refer to our migration guide. Click the link: our migration guide. Change from using URL Shortener to using Firebase Dynamic Links. See Figure 4.

For developers who want to migrate to FDL, please refer to our migration guide. Click the link: our migration guide. Change from using URL Shortener to using Firebase Dynamic Links.
Figure 4

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系