Go 語言~ 初學筆記

主要學習文件依照下面的連結閱讀
https://github.com/astaxie/build-web-application-with-golang
首先安裝Go,Go也有第三方套件做版本切換gvm.
https://github.com/moovweb/gvm
依照只是裝好後.

1
2
3
4
5
6
7
$ gvm install go1
Downloading Go source...
Installing go1...
 * Compiling...
$ gvm use go1
Now using version go1
$

接下來可以開始試用Go !!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ go 
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         run godoc on package sources
    env         print Go environment information
    fix         run go tool fix on packages
    fmt         run gofmt on package sources
    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:
 
    gopath      GOPATH environment variable
    packages    description of package lists
    remote      remote import path syntax
    testflag    description of testing flags
    testfunc    description of testing functions
 
Use "go help [topic]" for more information about that topic.

關於幾個重要系統環境變數.gvm都幫你切換好了!

1
2
3
4
5
6
7
GVM_ROOT=/Users/luosteve/.gvm
GVM_VERSION=1.0.16
GVM_PATH_BACKUP=/Users/luosteve/.gvm/bin:/Users/luosteve/.rvm/gems/ruby-1.9.3-p374/bin:/Users/luosteve/.rvm/gems/ruby-1.9.3-p374@global/bin:/Users/luosteve/.rvm/rubies/ruby-1.9.3-p374/bin:/Users/luosteve/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/go/bin:/Users/luosteve/mybin:/Users/luosteve/bin/google_appengine:/Users/luosteve/.rvm/bin
gvm_go_name=go1
gvm_pkgset_name=global
GOROOT=/Users/luosteve/.gvm/gos/go1
GOPATH=/Users/luosteve/.gvm/pkgsets/go1/global

再來是開發的$GOPATH環境變數
$GOPATH環境變數主要為工作目錄,可為多個.

1
    export GOPATH=~/apple/mygo1;~/apple/mygo2

以上 $GOPATH 目錄主要有三個內容:

  • src 原始碼(比如:.go .c .h .s等)
  • pkg 編譯後的包(比如:.a)
  • bin 編譯後的文件為可執行檔

依照範例建立一個sample,執行go install後會編譯好檔案.

1
2
3
4
-> % ll
total 8
-rw-r--r--  1 luosteve  staff  2108  2 19 10:36 mymath.a
luosteve@SteveMac [10:36:40] [~/Go/pkg/darwin_amd64]

在依照範例建立好mathapp,執行go build產生mathapp執行檔.
螢幕快照 2013-02-19 上午10.45.05