go环境配置与使用

go环境配置与使用

0. 环境配置

#!/bin/bash
#Author: erices
#Created Time: 2020/12/03
#Release: 1.0
#Script Description: golang install script

go_version="1.15.5.linux-amd64"
install_path="/opt/go"
# download golang on studygolang mirror
wget -c -t 0 -T 1200 https://studygolang.com/dl/golang/go${go_version}.tar.gz

tar zxvf go${go_version}.tar.gz

mv go $install_path

if cat ~/.bash_profile | grep 'export GOROOT=' >>/dev/null 2>&1; then
    echo "GOROOT is $GOROOT now"
    echo -e "if you want to change it ,run command :\n"
    echo "export GOROOT=${install_path} >>~/.bash_profile"
    echo "export GOPATH=${HOME}/gopath >>~/.bash_profile"
    echo "export PATH=${GOROOT}/bin:${GOPATH}/bin:$PATH >>~/.bash_profile"
else
    # set environment
    echo "export GOROOT=${install_path}" >>~/.bash_profile
    echo "export GOPATH=${HOME}/gopath" >>~/.bash_profile
    echo "export PATH=${GOROOT}/bin:${GOPATH}/bin:$PATH" >>~/.bash_profile
fi
# source the env setting
source ~/.bash_profile

# set go mod
go env -w GO111MODULE=on
# set go mod proxy
go env -w GOPROXY=https://goproxy.cn,direct
# show version
go version

echo "You need run \" source ~/.bash_profile \" again !"

1. go build

Go语言中使用 go build 命令主要用于编译代码。在包的编译过程中,若有必要,会同时编译与之相关联的包。

go build 有很多种编译方法,如无参数编译、文件列表编译、指定包编译等,使用这些方法都可以输出可执行文件。

go build 还有一些附加参数,可以显示更多的编译信息和更多的操作,详见下表所示。

附加参数备 注
-v编译时显示包名
-p n开启并发编译,默认情况下该值为 CPU 逻辑核数
-a强制重新构建
-n打印编译时会用到的所有命令,但不真正执行
-x打印编译时会用到的所有命令
-race开启竞态检测

hello.go

package main

import (
        "fmt"
        "hello/pkg"
)

func main() {
    pkg.CustomPkgFunc()
    fmt.Println("Hello World")
}

pkg.go

package pkg

import "fmt"

func CustomPkgFunc() {
    fmt.Println("call CustomPkgFunc")
}
➜  src tree
.
├── hello
│   ├── hello.go
│   └── pkg
│       └── pkg.go
└── main

2 directories, 3 files
➜  src go build -o main hello
➜  src ls
hello  main

# 编译多个go源文件必须要在同一个目录下
➜  src go build hello/hello.go hello/pkg/pkg.go
named files must all be in one directory; have hello/ and hello/pkg/
➜  src ls
hello
➜  src cd hello
➜  hello ls
hello.go  pkg
➜  hello go build hello.go pkg/pkg.go
named files must all be in one directory; have ./ and pkg/

2. go run

使用 go run 命令 - 在命令提示符旁,输入 go run workspacepath/src/hello/hello.go

上述命令中的 workspacepath 应该替换为你自己的工作区路径(Windows 下的 C:/Users/YourName/go,Linux 或 Mac 下的 $HOME/go)。

在控制台上会看见 Hello World 的输出。

3. go install

需要配置GOPATH路径,配置在当前项目路径,项目目录下需要包含bin和src目录,在src目录下管理源码

➜  src go install hello
➜  bin ./hello
call CustomPkgFunc
Hello World
➜  go tree
.
├── bin
│   └── hello
└── src
    └── hello
        ├── hello.go
        └── pkg
            └── pkg.go

4 directories, 3 files
➜  go cat ~/.zshrc|grep GOPATH
export GOPATH=/root/workspace/go
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值