最后安利 golang (不适合小白) https://golang.org/doc/#references (能看完重点章节 就算不错了 我还没看完哈哈)
订阅 golang weekly https://golangweekly.com/
https://github.com/avelino/awesome-go
Downloads - The Go Programming Language
Linux 下安装 Go
wget 链接
tar -C /usr/local -zxvf 压缩包
vim /etc/profile
# 最下面增加两行
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# 保存后source下
source /etc/profile
go versiongin 框架下载慢
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
go get -u -v github.com/gin-gonic/gin创建项目
初始化环境
go mod init 项目名
go get -u github.com/gin-gonic/gin@latest
go mod tidy优雅关闭 go web 程序
读 “优雅关闭的 Go Web 服务器” | Go 技术论坛
go 提供了 signal.Notify 函数,监听某些信号,然后通知某个端口。
func RunServer() {
r := RegisterRouter()
server := http.Server{
Addr: ":" + global.App.Config.App.Port,
Handler: r,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("server listen err:%s", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// 在此阻塞
<-quit
server.SetKeepAlivesEnabled(false)
ctx, channel := context.WithTimeout(context.Background(), 1*time.Second)
defer channel()
if err := server.Shutdown(ctx); err != nil {
fmt.Println("server shutdown error")
}
fmt.Println("server exiting...")
}go 错误
golang 踩坑之 - 服务的文件句柄超出系统限制(too many open files) | Zach Ke’s Notes