golang

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

安装

下载地址

https://golang.org/dl/ , 选择合适的版本

wget https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
tar zxvf go1.9.2.linux-amd64.tar.gz
mv go /usr/local/

配置环境变量

vi ~/.bashrc 或者 vi /etc/profile,然后 source ~/.bashrc

export GOROOT='/usr/local/go'
export GOPATH='/www/go'
export GOBIN=$GOPATH/bin
export PATH=$PATH:${GOROOT}/bin:${GOBIN}

查看版本信息

[root@localhost ~]# go version
go version go1.9.2 linux/amd64

目录结构

Golang项目目录下一般有如下目录:

├── README.md
├── bin 编译后生成的可执行文件
│   ├── bee
│   ├── main
├── pkg 编译后生成的文件
│   ├── darwin_amd64
│   └── linux_amd64
└── src 存放源代码
    ├── github.com
    └── main.com

常用命令

启动

go run xxx.go

常用命令

go  build       compile packages and dependencies
go  clean       remove object files
go  doc         show documentation for package or symbol
go  env         print Go environment information
go  bug         start a bug report
go  fix         run go tool fix on packages
go  fmt         run gofmt on package sources
go  generate    generate Go files by processing source
go  get         download and install packages and dependencies
go  install     compile and install packages and dependencies
go  list        list packages
go  run         compile and run Go program
go  test        test packages
go  tool        run specified go tool
go  version     print Go version
go  vet         run go tool vet on packages

IDE

http server

// 处理输出
func handler(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello,world")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":9001", nil)
}

参见

参考网址