1. 程式人生 > >Golang入門-----安裝配置篇

Golang入門-----安裝配置篇

Go語言是一種開源的程式語言,它旨在使我們能夠方便地構建簡單、 可靠、 高效的軟體。

安裝

# go1.4.3版本依賴於GCC環境

$ cd ~
$ mkdir {local,go}
$ tar -zxvf go1.4.3.src.tar.gz -C /home/go/local/ 
$ cd local
$ mv go/ go1.4.3/
$ cd go1.4.3/src
$ ./make.bash
$ tar -zxvf go1.8.linux-amd64.tar.gz -C /home/go/local/
$ cd ~/local/go/
$ export GOROOT_BOOTSTRAP=/home/go/local/go1.4.3
/ $ ./make.bash #配置環境變數 $ ~/.bashrc export GOROOT=$HOME/local/go export GOPATH=$HOME/go export PATH=$GOROOT/bin:$GOPATH/bin:$PATH $ source ~/.bashrc #驗證安裝是否成功 $go version #版本 go version go1.8 linux/amd64 $go env #環境變數 GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux"
GOPATH="/home/go/go" GORACE="" GOROOT="/home/go/local/go" GOTOOLDIR="/home/go/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build156894198=/tmp/go-build" CXX="g++" CGO_ENABLED="1" PKG_CONFIG="pkg-config" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2"

基本使用

1.基本語法

$ go --help
Go is a tool for managing Go source code.

Usage:

    go command [arguments]

The commands are:

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

Use "go help [command]" for more information about a command.

Additional help topics:

    c           calling between Go and C
    buildmode   description of build modes
    filetype    file types
    gopath      GOPATH environment variable
    environment environment variables
    importpath  import path syntax
    packages    description of package lists
    testflag    description of testing flags
    testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

2.測試案例

$ cd ~/go
$ vi hello.go
package main

import "fmt"

func main() {
        fmt.Println("Hello,golang")
}

$ go run hello.go 
hello world

3.跨平臺編譯
go可以實現一個平臺開發,編譯為多個平臺版本。同平臺隨意拷貝執行。

#windows平臺,可以放在windows環境執行        
$GOOS=windows go build -o hello.windows.exe helloworld.go 
#Mac平臺,可以放在Mac環境執行    
$GOOS=darwin go build -o hello.mac helloworld.go 
#Linux平臺
$GOOS=linux go build -o hello.linux helloworld.go 

4.格式化程式碼

#方法1:gofmt格式化
$gofmt -w hello.go

#方法2:goimports格式化程式碼,但是 goimports 不是自帶的,需要額外安裝,而且可以補齊缺少的import物件。
安裝goimports:
$ mkdir -p $GOPATH/src/golang.org/x
$ cd $GOPATH/src/golang.org/x
$ git clone https://github.com/golang/tools.git
$ go install golang.org/x/tools/cmd/goimports 

$ goimports -w hello.go

IDE工具選擇

1、Atom配合go-plus外掛。
2、LitelDE,專用IDE。
3、emacs + spacemacs配置
4、Eclipse等。

具體使用啥,熟悉哪個用哪個