Golang: Here We Go(1.安装&&脚本)

原文是 Golang 技术汇总,现在重新改变到 Gogolang 专栏内。

本文只讲开始写代码前的准备工作,安装&配置插件。

(保留上图,留作历史)

安装部分

初步打算用 VS Code 做 Golang 的 IDE 进行学习,后期做项目考虑用 ideaJ + golang插件。

安装配置

安装我选择的简单的方式,标准 pkg 安装(源码安装比较慢), 平台macOS。

实际上是安装到了 /usr/local/go/bin/go,并且环境变量同时也设置好了,挺好的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/merlin/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4x/87z_b5xn1c11y_8trtp98bkm0000gn/T/go-build363370741=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

主要解释一下: (关注主要的)

  • GOBIN 工作目录下的 bin 文件夹
  • GOEXE 生成可执行文件的后缀
  • GOHOSTARCH=”amd64”, GOHOSTOS=”darwin” 交叉编译相关
  • GOOS 当前系统的 OS 名称
  • GOPATH=”/Users/merlin/go” 就是你工作目录
  • GOROOT=”/usr/local/go”就是安装目录
  • GOTOOLDIR 工具目录

为了以后安装 Go 包&插件,以及 Go版本升级,上面的工作是不够的:

  • 建立专门的Go目录,下面分为三个子目录 : (经检验,还是设置 GOPATH为一个目录比较好)
    • 3rd 三方包或者软件 (不要这么做)
    • go 软链接指向相关版本 (不要这么做)
    • own 自己的工作目录
  • 修改 GOPATH 目录为自己的工作目录 (它给我建立的/Users/merlin/go不是我想要的,所以覆盖掉)
    • ``
  • 在 GOPATH 下建立三个文件夹
    • bin(生成的可执行文件)
    • pkg(以.a为后缀的包文件)
    • src(源码目录)

完整的代码,大概是:

1
2
3
4
5
6
7
8
9
10
11
12
## 当前目录: /Users/merlin/onedrive/Golang
## $ mkdir 3rd own ## 不好
$ mkdir own

## make a link for go to real dir
$ ln -sf /usr/local/go ./go

## Go编译器自动给你设置的 GOPATH 还是要做好的
ln -sf /Users/merlin/onedrive/Golang/own /Users/merlin/go

# 在 GOPATH 下建立三个文件夹
$ cd own && mkdir bin pkg src

配置的环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## go related
export GO_HOME=/Users/merlin/onedrive/Golang
export GOROOT=$GO_HOME/go
export GOBIN=$GOROOT/bin

# make a link for go to real dir
#export GOPATH=$GO_HOME/3rd:$GO_HOME/go:$GO_HOME/own
export GOPATH=$GO_HOME/own

export PATH=$GOPATH/bin:$GOBIN:$PATH


###for cross-compling go-code
#export GOOS=linux
#export GOARCH=amd64

配置完: (3rd 其实不太需要了,因为环境变量上面设置的,我主要使用own)

现在的环境变量大致上是:

插件安装

GO 插件安装,大致上有:

1
2
3
4
5
6
7
8
9
10
11
12
go get -v -d -u github.com/nsf/gocode
go get -v -d -u github.com/uudashr/gopkgs/cmd/gopkgs
go get -v -d -u github.com/ramya-rao-a/go-outline
go get -v -d -u github.com/acroca/go-symbols
go get -v -d -u golang.org/x/tools/cmd/guru
go get -v -d -u golang.org/x/tools/cmd/gorename
go get -v -d -u github.com/fatih/gomodifytags
go get -v -d -u github.com/josharian/impl
go get -v -d -u github.com/zmb3/gogetdoc
go get -v -d -u sourcegraph.com/sqs/goreturns
go get -v -d -u github.com/golang/lint/golint
go get -v -d -u github.com/cweill/gotests/...

当然你可以直接使用这个脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash

generate_name() {
date +%Y%m%d
}

get() {
export GOPATH=$PWD/gopath

mkdir -p $GOPATH

go get -v -d -u github.com/nsf/gocode
go get -v -d -u github.com/uudashr/gopkgs/cmd/gopkgs
go get -v -d -u github.com/ramya-rao-a/go-outline
go get -v -d -u github.com/acroca/go-symbols
go get -v -d -u golang.org/x/tools/cmd/guru
go get -v -d -u golang.org/x/tools/cmd/gorename
go get -v -d -u github.com/fatih/gomodifytags
go get -v -d -u github.com/josharian/impl
go get -v -d -u github.com/zmb3/gogetdoc
go get -v -d -u sourcegraph.com/sqs/goreturns
go get -v -d -u github.com/golang/lint/golint
go get -v -d -u github.com/cweill/gotests/...
}

pack() {
cd ./gopath
local name=$(generate_name)
zip -qr - . | pv > ../vscode_go_deps.$name.zip
cd ..
shasum -a 256 vscode_go_deps.$name.zip > vscode_go_deps.$name.sha256sum
}

unpack() {
if [ -z "$1" ]; then
echo "Usage: unpack <vscode_go_deps.zip> [GOPATH]"
return 1
fi

local gopath=${2:-$GOPATH}
if [ -z "$gopath" ]; then
echo "ERROR: Cannot find GOPATH to unzip the dependencies."
echo "Usage: unpack <vscode_go_deps.zip> [GOPATH]"
return 1
fi

local checksum=${1%%.zip}.sha256sum
if [ -f "$checksum" ]; then
shasum -a 256 -c $checksum
fi

unzip -q $1 -d $gopath
}

install() {
# export GOPATH=$PWD/gopath
# export GOBIN=$GOPATH/bin

go install github.com/nsf/gocode
go install github.com/uudashr/gopkgs/cmd/gopkgs
go install github.com/ramya-rao-a/go-outline
go install github.com/acroca/go-symbols
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
go install github.com/fatih/gomodifytags
go install github.com/josharian/impl
go install github.com/zmb3/gogetdoc
go install sourcegraph.com/sqs/goreturns
go install github.com/golang/lint/golint
go install github.com/cweill/gotests/...
}

clean() {
rm -rf ./gopath
}

command=$1
shift
$command "$@"

然后 sudo run.sh install, 生成完毕可以看到:

bin

btw: 也可以自己打包,使用 run.sh get 以及 run.sh pack,请直接看上面的脚本。

之后能使用的插件,大致上如下:(当然也可以直接在命令行里面使用)

  • Go: Add Import to add an import from the list of packages in your Go context
  • Go: Current GOPATH to see your currently configured GOPATH
  • Go: Test at cursor to run a test at the current cursor position in the active document
  • Go: Test Package to run all tests in the package containing the active document
  • Go: Test File to run all tests in the current active document
  • Go: Test Previous to run the previously run test command
  • Go: Test All Packages in Workspace to run all tests in the current workspace
  • Go: Generates unit tests for package Generates unit tests for the current package
  • Go: Generates unit tests for file Generates unit tests for the current file
  • Go: Generates unit tests for function Generates unit tests for the selected function in the current file
  • Go: Install Tools Installs/updates all the Go tools that the extension depends on
  • Go: Add Tags Adds configured tags to selected struct fields.
  • Go: Remove Tags Removes configured tags from selected struct fields.
  • Go: Generate Interface Stubs Generates method stubs for given interface
  • Go: Run on Go Playground Upload the current selection or file to the Go Playground

Merlin
2018.1月底 重温Golang


Merlin
2018.2月初 深入Golang, 发现Golang一点儿不完美,比起Cpp它避免了很多坑,但是如果真要完整处理起来恐怕会和cpp一样臃肿


Merlin 2018.3 改版成专栏,正式成为 Golang 专业户

文章目录
  1. 1. 安装部分
    1. 1.1. 安装配置
  2. 2. 插件安装
|