下载地址(官网):
https://golang.org/dl/
下载地址(国内):
https://dl.gocn.io/
想编译GO,必须先有一个GO的编译器。
创建GO的编译器:
[[email protected] ~]# mkdir local
[[email protected] ~]# cd local
[[email protected] local]# curl -LO https://dl.google.com/go/go1.4.3.src.tar.gz
[[email protected] local]# tar xvf go1.4.3.src.tar.gz
[[email protected] local]# mv go go1.4.3
[[email protected] local]# cd go1.4.3/
[[email protected] go1.4.3]# cd src/
[[email protected] src]# ./make.bash
[[email protected] go1.4.3]# bin/go version
go version go1.4.3 linux/amd64
安装GO软件:
[[email protected] local]# tar xf go1.10.3.src.tar.gz
[[email protected] local]# ls
go go1.10.3.src.tar.gz go1.4.3 go1.4.3.src.tar.gz
[[email protected] local]# mv go go1.10
[[email protected] go1.10]# cd src/
[[email protected] src]# ./make.bash
Building Go cmd/dist using /root/go1.4.
ERROR: Cannot find /root/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
需要设置1.4版本的环境变量
[[email protected] src]# export GOROOT_BOOTSTRAP=/root/local/go1.4.3
[[email protected] src]# ./make.bash
[[email protected] go1.10]# bin/go version
go version go1.10.3 linux/amd64
[[email protected] local]# mv go1.10 go
初始化环境
GOROOT 放置go的标准库和工具链
$HOME/local/go (linux, mac)
c:\local\go (windows)
GOPATH 放置第三方代码和自己的工程
$HOME/go (linux, mac)
c:\go (windows)
PATH
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
[[email protected] ~]# vim ~/.bashrc
export GOROOT=/root/local/go
export GOPATH=/root/go
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
测试一个Hello World
[[email protected] go]# vim hello.go
package main
import "fmt"
func main() {
fmt.Println("hello golang")
}
[[email protected] go]# go run hello.go
hello golang
编译运行
go build hello.go && ./hello # 编译成.exe的文件,在Windows中执行。直接编译成可执行文件。在Linux中也可以直接运行 ./hello
go run hello.go #在linux中执行执行该文件
[[email protected] go]# go build hello.go
[[email protected] go]# file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
[[email protected] go]# ./hello
hello golang
mac系统把程序编程成Linux系统的程序:
[[email protected] go]# GOOS=linux go build hello.go 变成linux
[[email protected] go]# GOOS=windows go build hello.go 变成windows
[[email protected] go]# GOOS=darwin go build hello.go 变成mac
[[email protected] go]# GOOS=linux go build -o hello.linux hello.go
[[email protected] go]# GOOS=linux go build -o hello.lin hello.go
[[email protected] go]# GOOS=windows go build -o hello.exe hello.go
[[email protected] go]# GOOS=darwin go build -o hello.mac hello.go
原文地址:https://www.cnblogs.com/green-frog-2019/p/11337452.html