环境搭建
- 要代理 export https_proxy=”132.122.237.162:52080”
- https://golang.org/dl/ 取得下载链接,wget 下载
- 环境变量:
```bash
vi ~/.bashrc 追加:
export PATH=${PATH}:/root/ch/go/bin
执行命令
source ~/.bashrc
- 代理参考 [https://goproxy.cn/](https://goproxy.cn/)```bash$ go env -w GO111MODULE=on$ go env -w GOPROXY=https://goproxy.cn,direct
修改 GOPATH,使用Go的包管理方式,依赖的第三方包被下载到了$GOPATH/pkg/mod路径下。
go env -w GOPATH=/root/ch/go/gopath
所有 go env 的配置保存在:/root/.config/go/env
HelloWorld
- 项目目录:/root/ch/project/helloworld
- main.go,拷贝 gin 框架教程里所述 ```go package main
import ( “net/http”
"github.com/gin-gonic/gin"
)
func main() { router := gin.Default() router.LoadHTMLGlob(“templates/“) router.GET(“/index”, func(c gin.Context) { c.HTML(http.StatusOK, “index.tmpl”, gin.H{ “title”: “Main website”, }) }) router.Run(“:8080”) }
- templates/index.tmpl```html<html><h1>{{ .title }}</h1></html>
- 初始化模块、下载依赖、编译
go mod init hellogo mod tidygo build main.go
参考
Go go.mod详解:https://blog.csdn.net/weixin_39003229/article/details/97638573
Gin:https://github.com/gin-gonic/gin#custom-template-renderer
