IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Centos7 go 安装

    瀚海星空发表于 2021-05-20 13:28:36
    love 0

    安装go

    下载地址:https://golang.org/dl/go1.16.4.linux-amd64.tar.gz

    下载后上传到指定目录,解压,配环境变量。

    [root@3 down]# tar zxvf go1.16.4.linux-amd64.tar.gz
    
    [root@3 down]# mv go /usr/local/go
    
    [root@3 ~]# vi .bashrc
    export PATH=$PATH:/usr/local/go/bin
    
    [root@3 ~]# source .bashrc
    [root@3 ~]# go version
    go version go1.16.4 linux/amd64
    
    [root@3 ~]# vi /etc/profile
    
    export GOROOT=/usr/local/go
    export GOPATH=/home/gopath
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    
    [root@3 ~]# mkdir -p /home/go/src /home/go/pkg /home/go/bin
    [root@3 ~]# source /etc/profile
    [root@3 ~]# go env
    ...
    
    

    写测试程序

    创建两个模块,一个greeting模块和一个主模块,在主模块调用greeting模块

    创建目录结构

    [root@3 ~]# mkdir go
    [root@3 ~]# cd go
    [root@3 ~]# mkdir -p hello greeting
    

    创建被调用模块

    [root@3 go]# cd greeting/
    [root@3 greeting]# go mod init z.com/greeting
    
    #  go mod init 创建一个go.mod文件来跟踪依赖 此时,会在当前目录下添加一个go.mod文件
    [root@3 greeting]# cat go.mod
    module z.com/greeting
    
    go 1.16
    [root@3 go]# vi greeting/greeting.go
    

    编辑 greeting.go

    package greeting
    
    import "fmt"
    
    func Greeting(name string) string {
            msg := fmt.Sprintf("hello %v", name)
            return msg
    }
    

    从另一个模块调用

    编辑主程序 hello.go

    [root@3 greeting]# cd ..
    [root@3 go]# cd hello
    [root@3 hello]# go mod init z.com/hello
    go: creating new go.mod: module z.com/hello
    go: to add module requirements and sums:
            go mod tidy
    
    [root@3 hello]# vi hello.go
    
    package main
    
    import "fmt"
    import "z.com/greeting"
    
    func main() {
            msg := greeting.Greeting("梦想家")
            fmt.Println(msg)
    }
    
    [root@3 hello]# go run hello.go
    hello.go:6:8: no required module provides package z.com/greeting; to add it:
            go get z.com/greeting
    
    // 因为没有发布模块,所以本地开发,需要修改一下依赖        
    [root@3 hello]# go mod edit -replace=z.com/greeting=../greeting
    [root@3 hello]# go run .
    hello.go:6:8: module z.com/greeting provides package z.com/greeting and is replaced but not required; to add it:
            go get z.com/greeting
            
    [root@3 hello]# go mod tidy
    go: found z.com/greeting in z.com/greeting v0.0.0-00010101000000-000000000000
    [root@3 hello]# cat go.mod
    module z.com/hello
    
    go 1.16
    
    replace z.com/greeting => ../greeting
    
    require z.com/greeting v0.0.0-00010101000000-000000000000
    
    [root@3 hello]# go run .
    hello 梦想家
    
    

    运行成功

    go module 功能被集成到 go 命令行工具中,例如,在调用诸如 go build,go install,go run,go test 之类的命令时,将启动相应的操作,如缓存,创建或更新 go.mod 和 go.sum 等

    go fmt 格式化文档。go mod -fix 更新依赖,删除一些go.mod的重复项等。

    配置国内代理

    国内网络访问国外资源经常会出现不稳定的情况。 Go 生态系统中有着许多中国 Gopher 们无法获取的模块,比如最著名的 golang.org/x/…。并且在中国大陆从 GitHub 获取模块的速度也有点慢。

    因此设置 CDN 加速代理就很有必要了,以下是几个速度不错的提供者:

    • 七牛:Goproxy 中国 https://goproxy.cn
    • 阿里: mirrors.aliyun.com/goproxy/
    • 官方: 全球 CDN 加速 https://goproxy.io/
    • 其他:jfrog 维护 https://gocenter.io

      七牛proxy

      全球不限速代理。 打开你的终端并执行 使用go1.11以上版本并开启go module机制

      $ go env -w GO111MODULE=on
      $ go env -w GOPROXY=https://goproxy.cn,direct
      

      或

      $ echo "export GO111MODULE=on" >> ~/.profile
      $ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
      $ source ~/.profile
      

      官方

      # 配置 GOPROXY 环境变量
      export GOPROXY=https://goproxy.io,direct
      # 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
      # export GOPRIVATE=git.mycompany.com,github.com/my/private
      

      阿里

    # 使用go1.11以上版本并开启go module机制
    export GOPROXY=https://mirrors.aliyun.com/goproxy/
    

    安装tour教程

    [root@3 ~]# go get golang.org/x/tour
    [root@3 ~]# tour
    2021/05/20 14:31:43 Serving content from /home/go/pkg/mod/golang.org/x/tour@v0.0.0-20210512164546-a278aee398d5
    2021/05/20 14:31:43 Please open your web browser and visit http://127.0.0.1:3999
    


沪ICP备19023445号-2号
友情链接