Runtime Error: Invalid memory address or nil pointer dereference when using the URL shortener API
1. Reference URL: https://github.com/unknwon/the-way-to-go_zh_en/blob/master/ebook/09.11.md . Test by browsing the page of http://localhost:8080/. Enter: http://www.destandaard.be in shorten this, and click: Give me the short URL. Loading until timeout. as shown in Figure 1
2. Check the terminal output and report an error: Runtime Error: Invalid memory address or nil pointer dereference. as shown in Figure 2
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.(*servmux).serveHttp(0xe6f740, 0xac7dc0, 0xc0002ac1c0, 0xc00029a100) c:/go/src/net/http/server.go:2387 +0x1c4 NET/HTTP.ServerHandler.Server.servHttp(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.(*servmux).servehttp(0xe6f740, 0xac7dc0, 0xc00030c000, 0xc0002b0100) c:/go/src/net/http/server.go:2387 +0x1c4 NET/HTTP.ServerHandler.Server.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.Server.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. Check the Go file, urlshortener.go. For the simplicity of the code, we do not detect the returned error status, but we must do the detection in the application of the real production environment. Open the URL: http://goo.gl and find that by March 30, 2019, the URL shortener API has stopped supporting. as shown in Figure 3
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 roottmltmpl = template.must(template.new("roothtml").parse(`
<html><body>
<h1>url shortener</h1>
{{if .}}{{.}}<br /><br />{{end}}
<form action="/short" type="post">
shorten this: <input type="text" name="longurl" />
<input type="submit" value="give me the short url" />
</form>
<br />
<form action="/long" type="post">
expand this: http://goo.gl/<input type="text" name="shortURL" />
<input type="submit" value="give me the long url" />
</form>
</body></html>
`))
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()
roottmltmpl.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
}
roottmltmpl.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 to use Firebase dynamic links instead of using URL shortener. as shown in Figure 4



