1、参考网址:https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.11.md 。通过浏览 http://localhost:8080/ 的页面来测试。在 Shorten this 中输入:http://www.destandaard.be ,点击:Give me the short URL。一直加载中,直至超时。如图1

2、查看终端输出,报错:runtime error: invalid memory address or nil pointer dereference。如图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.(*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、查看 Go 文件,urlshortener.go。为了代码的简洁我们并没有检测返回的错误状态,但是在真实的生产环境的应用中一定要做检测。打开网址:http://goo.gl ,发现到 2019年3月30日,URL Shortener API 已经停止支持。如图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 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、对于希望迁移到 FDL 的开发人员,请参阅我们的迁移指南。点击链接:our migration guide。由使用 URL Shortener 改为使用 Firebase 动态链接。如图4

需要长期技术维护或远程问题排查?
我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。
如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:
- ✅ PHP / Laravel / Yii2 老项目无人维护
- ✅ Go / Gin 后端接口需要排查或优化
- ✅ WordPress 网站访问慢、报错或插件冲突
- ✅ Nginx / MySQL / Redis / Linux 服务器异常
- ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
- ✅ 需要长期远程技术支持或兼职维护
更多介绍请查看:关于我 & 合作
微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论
一条对“使用 URL Shortener API 时,runtime error: invalid memory address or nil pointer dereference”的回复
Люди подскажите А бюрократия эта просто выносит мозг Кто-то год собирает справки о родственниках Короче, реально толковые ребята — бюро репатриации москва с гарантией Подали в консульство без ошибок В общем, вся инфа вот здесь — агентство репатриации https://grazhdanstvo-izrailya.ru Не мучайтесь с бюрократией сами Перешлите тому кто думает о репатриации