macOS:VScode
CentOS7:go
1 CentOS7:安装go和git
1.1 安装go
从go语言中文社区下载go语言运行环境
下载相应的平台版本
[root@hbhost temp]# wget https://studygolang.com/dl/golang/go1.15.1.linux-amd64.tar.gz
[root@hbhost temp]# tar -zxvf go1.15.1.linux-amd64.tar.gz -C /usr/
[root@hbhost temp]# mkdir -p /home/go/gopath
[root@hbhost temp]# vim /etc/profile
export GOROOT=/usr/go
export GOPATH="/home/go/gopath:/home/go/go_workspace"
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
[root@hbhost temp]# source /etc/profile
[root@hangzhou01 program]# go version
go version go1.15.1 linux/amd64
[root@hangzhou01 gopath]# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/go/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/go/gopath:/home/go/go_workspace"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build232332625=/tmp/go-build -gno-record-gcc-switches"
1.2 安装git 2.28.0
[root@hbhost ~]# yum install -y gcc gcc-c++ zlib zlib-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
//支持https
[root@hbhost ~]# yum install -y curl-devel
[root@hbhost ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.28.0.tar.gz
[root@hbhost ~]# tar -zxvf git-2.28.0.tar.gz
[root@hbhost ~]# ./configure --prefix=/usr/git && make && make install
[root@hbhost ~]# cd /usr/git/bin && ./git --version
[root@hbhost ~]# vim /etc/profile
export GITROOT=/usr/git
export PATH=$GITROOT/bin:$PATH
[root@hbhost ~]# source /etc/profile
2 macOS:安装vscode
Download
https://vscode.cdn.azure.cn/stable/a0479759d6e9ea56afa657e454193f72aef85bd0/VSCode-darwin-stable.zip
安装插件Chinese (Simplified) Language Pack for Visual Studio Code、Remote Development
远程连接
设置远程连接ssh公钥认证
ssh-keygen
ls .ssh/
ssh-copy-id root@hb01
ssh root@hb01
3 CentOS7:安装插件
- go
- Ctrl + Shift + P
- Go: Install/Update Tools
建议在外网下载插件,然后拷贝至 $GOPATH/src
go get -u -v
cd $GOPATH
go install 安装插件
go install github.com/mdempsky/gocode
go install github.com/uudashr/gopkgs/v2/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/cweill/gotests/...
go install github.com/fatih/gomodifytags
go install github.com/josharian/impl
go install github.com/davidrjenni/reftools/cmd/fillstruct
go install github.com/haya14busa/goplay/cmd/goplay
go install github.com/godoctor/godoctor
go install github.com/go-delve/delve/cmd/dlv
go install github.com/stamblerre/gocode
go install github.com/rogpeppe/godef
go install golang.org/x/tools/cmd/goimports
go install golang.org/x/lint/golint
go install golang.org/x/tools/gopls
重启vscode。执行main运行测试:F5
修改监控文件数量
[root@hbhost ~]# cat /proc/sys/fs/inotify/max_user_watches
[root@hbhost ~]# vim /etc/sysctl.conf
fs.inotify.max_user_watches = 81920
[root@hbhost ~]# sysctl -p
4 hello world
[root@hbhost ~]# cd $GOPATH/src
[root@hbhost ~]# mkdir -p ch1/main
[root@hbhost src]# cat $GOPATH/src/ch1/main/hello_world.go
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Args)
if len(os.Args) > 1 {
fmt.Println("Hello World", os.Args[0])
fmt.Println("Hello World", os.Args[1])
} else {
fmt.Println("Hell World")
}
os.Exit(-1)
}
[root@hbhost src]# go run $GOPATH/src/ch1/main/hello_world.go
[/tmp/go-build707549298/b001/exe/hello_world]
Hell World
exit status 255
[root@hbhost src]# go run $GOPATH/src/ch1/main/hello_world.go Sam
[/tmp/go-build986507249/b001/exe/hello_world Sam]
Hello World /tmp/go-build986507249/b001/exe/hello_world
Hello World Sam
exit status 255
[root@hbhost ch1]# go build $GOPATH/src/ch1/main/hello_world.go
[root@hbhost ch1]# ls
hello_world main
[root@hbhost ch1]# ./hello_world
[./hello_world]
Hell World
[root@hbhost ch1]# ./hello_world Will
[./hello_world Will]
Hello World ./hello_world
Hello World Will
注意事项:
- 应用程序入口 必须是main包(package main,目录可以不是main)、main方法(func main(),文件名不一定是main.go)
- 返回值
func main() 不支持任何返回值
使用os.Exit来返回状态,如os.Exit(-1) - 传参
func main() 不支持传入参数
在程序中直接通过os.Args获取命令行参数,如os.Args[0] os.Args[1] - 运行
go run hello_world.go - 编译
go build hello_world.go 默认静态链接,生成独立的可执行二进制文件
./hello_world Tom 执行二进制文件
5 test
package first
import (
"fmt"
"testing"
)
func TestFirstTry(t *testing.T) {
t.Log("My first try!")
firstTry("this is a test!")
fmt.Printf("abc")
}
func firstTry(s string) {
fmt.Println(s, "")
fmt.Println("I am testing...!")
}
go test first_test.go
go test -v first_test.go