Like Share Discussion Bookmark Smile

J.J. Huang   2020-10-27   Golang   瀏覽次數:次   DMCA.com Protection Status

Go - 第一章 | 安裝Golang

前言

因為使用的macOS的環境,所以會著重在這方面做介紹,當然其他的OS環境也會大概提一下。

下載/安裝

下載

官方介紹提供了LinuxMacWindows的安裝檔下載連結

  • 下載
  • 執行安裝檔

註:我本機使用的安裝方式為HomeBraw;所以這邊不特別說明安裝檔的安裝方式。

HomeBraw安裝

  • 安裝 Homebrew
    1
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • 更新 Homebrew
    1
    brew update
  • 安裝 Golang
    1
    brew install golang

環境變數

  • 設定環境變數
    1
    2
    3
    export GOPATH="${HOME}/go"
    export GOROOT="/usr/local/Cellar/go/1.15.2/libexec"
    export PATH="${PATH}:${GOPATH}:${GOPATH}/bin:"

    註:GOROOT參數是Go安裝目錄,GOPATH參數是Go的工作目錄,此路徑不能跟GOROOT相同,且GOPATH允許多個目錄,當有多個目錄時,請注意分隔符號。

  • 更新生效環境變數
    1
    source .bash_profile

測試

  • 指令確認
    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    morose@localhost:~ $ go
    Go is a tool for managing Go source code.

    Usage:

    go <command> [arguments]

    The commands are:

    bug start a bug report
    build compile packages and dependencies
    clean remove object files and cached files
    doc show documentation for package or symbol
    env print Go environment information
    fix update packages to use new APIs
    fmt gofmt (reformat) package sources
    generate generate Go files by processing source
    get add dependencies to current module and install them
    install compile and install packages and dependencies
    list list packages or modules
    mod module maintenance
    run compile and run Go program
    test test packages
    tool run specified go tool
    version print Go version
    vet report likely mistakes in packages

    Use "go help <command>" for more information about a command.

    Additional help topics:

    buildconstraint build constraints
    buildmode build modes
    c calling between Go and C
    cache build and test caching
    environment environment variables
    filetype file types
    go.mod the go.mod file
    gopath GOPATH environment variable
    gopath-get legacy GOPATH go get
    goproxy module proxy protocol
    importpath import path syntax
    modules modules, module versions, and more
    module-get module-aware go get
    module-auth module authentication using go.sum
    module-private module configuration for non-public modules
    packages package lists and patterns
    testflag testing flags
    testfunc testing functions

    Use "go help <topic>" for more information about that topic.

HelloWorld

  • 隨意路徑下建立hello.go

    1
    2
    3
    morose@localhost:~ $ cd Documents/Temp
    morose@localhost:Documents/Temp $ touch hello.go
    morose@localhost:Documents/Temp $
  • vim編輯

    1
    morose@localhost:Documents/Temp $ vim hello.go

    hello.go內容:

    1
    2
    3
    4
    5
    package main
    import "fmt"
    func main() {
    fmt.Println("hello world")
    }
  • 執行hello.go

    1
    2
    morose@localhost:Documents/Temp $ go run ./hello.go
    hello world

結語

簡單的說明Golang的安裝和第一個程式(HelloWorld)的範例。一切的開始就是把環境先準備好,才能繼續往下一步。


註:以上參考了
Golang Documentation