> For the complete documentation index, see [llms.txt](https://minilabmemo.gitbook.io/golang-memo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minilabmemo.gitbook.io/golang-memo/basic/go-module.md).

# Go Module

Golang 在 1.11 開始就推出了 Go Module，我是從這邊就直接使用 Go Module了

### go Module 語法

幾個與moudule 有關的指令與參數紀錄

```
go mod init [module_path]
go mod tidy 添加需要用到但go.mod中查不到的模块,删除未使用的模块
go mod download
go mod graph
go mod why
go env -w GOFLAGS=-mod=mod

go help get usage: go get [-d] [-t] [-u] [-v] [-insecure] [build flags] [packages]
go get github.com/fatih/color@v1.8.0. # 下載特定版本的 go package
```

如果你看到以下錯誤訊息，表示沒有先下go mod init

```
//go.mod file not found in current directory or any parent directory; see 'go help modules'
```

#### go mod init \[module\_path]

這個後面位置可以是`github.com/username/myproject`，並不需上傳就可以設定，之後可以再上傳到github上

{% hint style="info" %}
Go 1.14 的 `Go Modules` 已經是正式版本， 來管理套件。就無需擔心 `$GOPATH` 與專案放置的位置。專案中的 `go.mod` 檔案假設你的專案放置在 GitHub 中，但這不是必須的。模組路徑可以是任意位址
{% endhint %}

#### go ​clean

第三方套件將會放置在`＄GOPATH/pkg/mod` directory.

* go ​clean `-modcache`

```
//$go ​clean -modcache 指令將會刪除mod資料夾
The -modcache flag causes clean to remove the entire module
download cache, including unpacked source code of versioned
dependencies.
```

###

### 參考

| reference                                                                                                                                                                                                                                                                                           |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 官方Go Modules Reference <https://go.dev/ref/mod>                                                                                                                                                                                                                                                     |
| <p>go mod graph 可视化——gmchart<br><a href="https://segmentfault.com/a/1190000038897207"><https://segmentfault.com/a/1190000038897207></a></p>                                                                                                                                                         |
| [**module path** 與 **import path**](https://mileslin.github.io/2020/08/Golang/%E5%88%B0%E5%BA%95-go-get-%E7%9A%84%E7%89%88%E8%99%9F%E6%80%8E%E9%BA%BC%E9%81%8B%E4%BD%9C%E7%9A%84/#%E5%A6%82%E6%9E%9C%E7%9B%AE%E6%A8%99-Repository-%E6%B2%92%E6%9C%89%E5%95%9F%E7%94%A8%E7%9A%84-Go-Module) **版次說明** |
