环境搭建

  • 要代理 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

  1. - 代理参考 [https://goproxy.cn/](https://goproxy.cn/)
  2. ```bash
  3. $ go env -w GO111MODULE=on
  4. $ go env -w GOPROXY=https://goproxy.cn,direct
  • 修改 GOPATH,使用Go的包管理方式,依赖的第三方包被下载到了$GOPATH/pkg/mod路径下。

    1. 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”

  1. "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”) }

  1. - templates/index.tmpl
  2. ```html
  3. <html>
  4. <h1>
  5. {{ .title }}
  6. </h1>
  7. </html>
  • 初始化模块、下载依赖、编译
    1. go mod init hello
    2. go mod tidy
    3. go 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