# \[Start! ->] Install Go

### 安裝三步驟

1. 去官網下載msi安裝go

確認安裝，開啟終端機輸入go version

```
>go version
go version go1.17.6 windows/amd64
```

安裝完後會有預設的環境變數，可以用自己習慣的環境變數更改．

2\. 下載IDE vscode \[個人習慣用IDE]

3\. 開啟vscode 安裝插件extension Go

右下角會要求你安裝周邊的go tool。 會被安裝在 $GOPATH\bin下。

\=============================

### 預設環境變數說明

```
GOROOT // ＧＯ的安裝資料夾位置，之後要升級就改這個資料夾
GOBIN // 安裝的可執行檔
GOPATH //程式碼位置，建議更改到自己習慣的資料夾
```

### (windows)-自行更改系統環境變數

* 預設環境變數(Windows)

```
set GOBIN=
set GOPATH=C:\Users\<your name>\go
set GOROOT=C:\Program Files\Go 
set GOMODCACHE=C:\Users\<your name>\go\pkg\mod
PATH:=%USERPROFILE%\go\bin 
預設%USERPROFILE%就是C:\Users\<your name>
```

如果有執行過安裝Tool工具，就會發現C:\Users\xxx\go資料夾被新增，且內部還有bin/pkg的資料夾。

自己的放置習慣

```
GOPATH=D:\go
GOBIN=%GOPATH%\bin
PATH 加入%GOBIN% //要加這段，才可以執行執行工具安裝好的指令 

D:\go\src 程式碼區
D:\go\bin 安裝工具，資料夾會自動建立
D:\go\pkg 安裝相依庫，資料夾會自動建立。
```

### (MAC)配置環境變數 <a href="#mac-an-zhuang-bi-ji" id="mac-an-zhuang-bi-ji"></a>

* vi \~/.bash\_profile

```
//輸入a編輯
export GOROOT="/usr/local/go"
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin 
export PATH=$PATH:$GOROOT/bin

//GOROOT表示GO安裝的目錄
//GOPATH是自訂想要放置程式的地方
//打完後esc輸入：wq存擋
```

* 執行 bash profile

```
source ~/.bash_profile
```

{% hint style="info" %}
開始寫個簡單的範例main.go

檢查autoimport或是autocompelte會不會運作，開發環境就完成了。

*<mark style="color:purple;">有的時候無法自動import可能只是func.name打錯字了=.=</mark>*
{% endhint %}

#### Bin資料夾被安裝的工具

這些工具可以幫助你在IDE上面的開發自動補全等等 see \`官方 [golang/tools](https://github.com/golang/tools)\`

```
D:\go\bin>dir /B
dlv-dap.exe
dlv.exe
go-outline.exe
goimports.exe
gomodifytags.exe
gopkgs.exe
goplay.exe
gopls.exe
gotests.exe
impl.exe
staticcheck.exe
```

### 升版降版

通常我在win環境都是直接更換安裝目錄c:/go

程式面debug工具會需要重新更新一下

### vscode IDE

* 如果你用[vscode插件-go](https://github.com/golang/vscode-go/wiki/features#diagnostics) go build/ go vet/ golint等等會包在裡面了，撰寫時有問題會出現在提示或者問題裡面
* language-server 支援編輯器或 IDE 中程式設計語言的原始程式碼自動完成或 移至定義 等豐富編輯功能

#### 參考文章

| refer                                                                               | note |
| ----------------------------------------------------------------------------------- | ---- |
| [Day4 源碼組成與Go tools](https://ithelp.ithome.com.tw/articles/10217436)                | TBD  |
| [VSCode 支持Go](https://geek-docs.com/vscode/vscode-tutorials/vscode-support-go.html) | 安裝方式 |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://minilabmemo.gitbook.io/golang-memo/basic/kai-shi-go-lv-cheng-install-go.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
