iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 3
0
自我挑戰組

golang初探系列 第 3

day3-Go指令簡述及Go Module 運用

  • 分享至 

  • twitterImage
  •  

延續上一章節的話題 GOPATH 及 Go Modulele 環境變量設定好之後
首先要了解開發中會使用到的指令

$ 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.

這邊列出幾個比較常用的指令

  • bug 該指令會打開默認的瀏覽器,並將連結至 golang github issue畫面上,並帶有 go env的環境資訊,用於回報錯誤
  • build 將撰寫程式碼,編譯成可執行檔,存放至當前目錄或指定目錄下
  • clean 清除臨時目錄或文件
  • env 列印出 go 環境變數訊息
  • fmt 用於程式碼格式化統一 code style (使用GOROOT/bin/gofmt 命令來實現功能)
  • get 用於下載依賴包
  • install 用於安裝依賴包,並在 GOPATH/bin 下增加執行檔
  • run 用於運行撰寫程式碼
  • tool 特殊工具(pprof、trac 用於優化程式碼非常有用,後續有機會在玩玩看)

GOPATH 資料結構

GOPATH 是工作(開發)路徑,也就是未來要開發專案(程式碼)都會在這個資料夾底下,
那資料夾結構應該要設置什麼樣子呢?
需要再 GOPATH/ 下創建3個資料夾
分別如下:

  • src 大部分專案撰寫上都放置該資料夾
  • bin 用於在 go install 依賴包,生成的執行檔放置位置
  • pkg 用於在 go get 依賴包下載存放位置

如何使用 Go modulde 進行開發

在開發過程中,在專案上常常經常使用其他開源的套件協助開發。
若沒有使用套件管理,協同開發者剛接觸專案時,可能面臨無法快速 build 起專案
或者是在開發階段上沒問題,上了正式機有問題。
種種原因之下,我們可以瞭解到使用套件管理是多麼重要的事
假設使用 github 創建 go-ironman repo
那開發路徑設置上 GOPATH/src/github.com/{github account}/go-iroman 目錄下進行開發

$ go mod init github.com/derek/go-iroman #啟用 go modulde 並取名為github.com/derek/go-iroman 

https://ithelp.ithome.com.tw/upload/images/20200903/20129671bnx3xvsHik.png
由上圖可以觀看出新增 go.mod
go.mod 記錄著 go 版本 及 module 名稱,因目前沒有安裝套件,並沒有紀錄套件及套件版本
https://ithelp.ithome.com.tw/upload/images/20200903/20129671av806eeMgw.png
這邊先安裝 gin 依賴套件
來看看發生甚麼事情呢

go get -u github.com/gin-gonic/gin #下載 最新版的 gin 

可以看出 go.mod 內容更新了
https://ithelp.ithome.com.tw/upload/images/20200904/201296711am2BQejvC.png
從 go.sum 中可以觀看出紀錄依賴的套件的版本
https://ithelp.ithome.com.tw/upload/images/20200904/2012967142wrdXsvma.png
發現 GOPATH/pkg/ 路徑下看到新增了一個 mod 資料夾
mod 資料夾存放著剛剛下載下來的套件
https://ithelp.ithome.com.tw/upload/images/20200904/20129671dVnqTXMY9h.png

這樣簡單的方式,使用了 go mod init,後續在專案上使用的套件,都會紀錄在go mod 上
簡單的指令就可以使開發專案上更穩定

補充資料

Go 語言 1.11 版本推出 go module


上一篇
day2-安裝及環境設置
下一篇
day4 -Golang 開發注意事項及語言規範
系列文
golang初探30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言