golang hello world

安装golang

下载

wget -c https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.7.1.linux-amd64.tar.gz

vim ~/.bash_profile

export PATH=$PATH:/usr/local/go/bin

测试

go version  

设置gopath环境变量

mkdir ~/gocode
vim ~/.bash_profile

新增以下内容

export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

导入环境变量

source ~/.bash_profile

hello world

mkdir $GOPATH/bin 
mkdir $GOPATH/pkg 
mkdir $GOPATH/github.com/user/hello -p
cd $GOPATH/github.com/user/hello

vim hello.go

package main

import "fmt"

func main() {
        fmt.Printf("Hello, world.\n")
}

go build # 构建(可执行程序在$GOPATH/github.com/user/hello) go install # 安装(可执行程序在$GOPATH/bin) hello # 运行