在Windows上进行WSL的语言安装和卸载

总结

在Windows中,谈论在WSL上安装Go语言时的设置和其他事项。

(Translation: Talking about settings and other matters when installing Go language on WSL in Windows.)

准备好

安装 WSL

操作顺序

按照公式安装步骤进行操作。

使用Windows MSI安装程序

image.png
image.png
image.png
image.png
image.png

确认安装

C:\Users\sampleuser>go env GOROOT
c:\go

C:\Users\sampleuser>go env GOPATH
C:\Users\sampleuser\go

# 環境変数 に入ってるのは GOPATH のみ(GOROOT は設定されない)
C:\Users\sampleuser>set | findstr -i "GOPATH"
GOPATH=C:\Users\sampleuser\go

C:\Users\sampleuser>set | findstr -i "GOROOT"

# PATH に $GOPATH/bin が追加される
C:\Users\sampleuser>set | findstr -i "PATH"
GOPATH=C:\Users\sampleuser\go

# PATH の最後に追加されている
Path=C:\Users\sampleuser\go\bin

GOPATH指的是什么?

外部包被安装到的目录。外部包的安装使用 go get 命令。

“$GOPATH/bin是什么?”

用于存放外部包的可执行二进制文件的目录。
换句话说,如果将路径设置为此处,就可以执行外部包的二进制文件。

尝试安装外部库来验证实际情况。

# -u はパッケージとその依存パッケージをアップデートする
C:\Users\sampleuser>go get -u github.com/motemen/gore/cmd/gore
go: finding github.com/motemen/gore v0.4.1
go: downloading github.com/motemen/gore v0.4.1
go: extracting github.com/motemen/gore v0.4.1
go: finding golang.org/x/text v0.3.2
go: finding github.com/motemen/go-quickfix latest
go: finding github.com/peterh/liner v1.1.0
go: finding golang.org/x/tools latest
go: downloading golang.org/x/text v0.3.2
go: finding github.com/mitchellh/go-homedir v1.1.0
go: downloading github.com/mitchellh/go-homedir v1.1.0
go: extracting github.com/mitchellh/go-homedir v1.1.0
go: downloading github.com/peterh/liner v1.1.0
go: extracting github.com/peterh/liner v1.1.0
go: downloading golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b
go: downloading github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679
go: extracting golang.org/x/text v0.3.2
go: extracting github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679
go: extracting golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b
go: downloading github.com/mattn/go-runewidth v0.0.3
go: extracting github.com/mattn/go-runewidth v0.0.3
go: finding github.com/mattn/go-runewidth v0.0.4
go: downloading github.com/mattn/go-runewidth v0.0.4
go: extracting github.com/mattn/go-runewidth v0.0.4

当查看 $GOPATH 目录下的内容时,会发现 bin 和 pkg 目录中有各种文件。但在某些情况下,如果不是最新的版本,则可能还会存在名为 src 的目录。

树形结构(很长)。可以看到每个包的版本的位置。C:\Users\sampleuser>tree go
C:\USERS\SAMPLEUSER\GO
├─bin
└─pkg
├─mod
│ ├─cache
│ │ └─download
│ │ ├─github.com
│ │ │ ├─mattn
│ │ │ │ └─go-runewidth
│ │ │ │ └─@v
│ │ │ ├─mitchellh
│ │ │ │ └─go-homedir
│ │ │ │ └─@v
│ │ │ ├─motemen
│ │ │ │ ├─go-quickfix
│ │ │ │ │ └─@v
│ │ │ │ └─gore
│ │ │ │ └─@v
│ │ │ └─peterh
│ │ │ └─liner
│ │ │ └─@v
│ │ ├─golang.org
│ │ │ └─x
│ │ │ ├─crypto
│ │ │ │ └─@v
│ │ │ ├─net
│ │ │ │ └─@v
│ │ │ ├─sync
│ │ │ │ └─@v
│ │ │ ├─sys
│ │ │ │ └─@v
│ │ │ ├─text
│ │ │ │ └─@v
│ │ │ ├─tools
│ │ │ │ └─@v
│ │ │ └─xerrors
│ │ │ └─@v
│ │ └─sumdb
│ │ └─sum.golang.org
│ │ ├─lookup
│ │ │ ├─github.com
│ │ │ │ ├─mattn
│ │ │ │ ├─mitchellh
│ │ │ │ ├─motemen
│ │ │ │ └─peterh
│ │ │ └─golang.org
│ │ │ └─x
│ │ └─tile
│ │ └─8
│ │ ├─0
│ │ │ └─x001
│ │ │ └─230.p
│ │ ├─1
│ │ │ └─004.p
│ │ └─2
│ │ └─000.p
│ ├─github.com
│ │ ├─mattn
│ │ │ ├─go-runewidth@v0.0.3
│ │ │ └─go-runewidth@v0.0.4
│ │ ├─mitchellh
│ │ │ └─go-homedir@v1.1.0
│ │ ├─motemen
│ │ │ ├─go-quickfix@v0.0.0-20160413151302-5c522febc679
│ │ │ │ ├─cmd
│ │ │ │ │ └─goquickfix
│ │ │ │ └─testdata
│ │ │ │ ├─general
│ │ │ │ ├─importname
│ │ │ │ ├─rangestmt
│ │ │ │ └─revert
│ │ │ └─gore@v0.4.1
│ │ │ ├─cli
│ │ │ ├─cmd
│ │ │ │ └─gore
│ │ │ ├─doc
│ │ │ └─gocode
│ │ └─peterh
│ │ └─liner@v1.1.0
│ │ └─.github
│ └─golang.org
│ └─x
│ ├─text@v0.3.2
│ │ ├─cases
│ │ ├─cmd
│ │ │ └─gotext
│ │ │ └─examples
│ │ │ ├─extract
│ │ │ │ └─locales
│ │ │ │ ├─de
│ │ │ │ ├─en-US
│ │ │ │ └─zh
│ │ │ ├─extract_http
│ │ │ │ ├─locales
│ │ │ │ │ ├─de
│ │ │ │ │ ├─en
│ │ │ │ │ ├─en-US
│ │ │ │ │ └─zh
│ │ │ │ └─pkg
│ │ │ └─rewrite
│ │ ├─collate
│ │ │ ├─build
│ │ │ └─tools
│ │ │ └─colcmp
└─sumdb
└─sum.golang.org

C:\Users\sampleuser\go\bin>dir
2019/10/15  17:01         9,094,656 gore.exe

卸载

image.png

请确认是否要删除安装。

C:\Users\sampleuser>go env GOPATH
'go' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。

追加到 GOPATH 和 PATH 中的环境变量也会被清除。

C:\Users\sampleuser>set | findstr -i "GOPATH"

安装WSL

image.png
image.png

我将解压到 /usr/local 目录下。

unix_user:~$ sudo tar -C /usr/local -xzf /mnt/c/Users/sampleuser/go1.13.1.linux-amd64.tar.gz
[sudo] password for unix_user:

# 確認
unix_user:~$ ls -la /usr/local/go
total 408
drwxr-xr-x 1 root root  4096 Sep 26 03:53 .
drwxr-xr-x 1 root root  4096 Oct 15 17:57 ..
-rw-r--r-- 1 root root 55389 Sep 26 03:52 AUTHORS
-rw-r--r-- 1 root root  1339 Sep 26 03:52 CONTRIBUTING.md
-rw-r--r-- 1 root root 84339 Sep 26 03:52 CONTRIBUTORS
-rw-r--r-- 1 root root  1479 Sep 26 03:52 LICENSE
-rw-r--r-- 1 root root  1303 Sep 26 03:52 PATENTS
-rw-r--r-- 1 root root  1607 Sep 26 03:52 README.md
-rw-r--r-- 1 root root   397 Sep 26 03:52 SECURITY.md
-rw-r--r-- 1 root root     8 Sep 26 03:53 VERSION
drwxr-xr-x 1 root root  4096 Sep 26 03:53 api
drwxr-xr-x 1 root root  4096 Sep 26 03:55 bin
drwxr-xr-x 1 root root  4096 Sep 26 03:53 doc
-rw-r--r-- 1 root root  5686 Sep 26 03:52 favicon.ico
drwxr-xr-x 1 root root  4096 Sep 26 03:53 lib
drwxr-xr-x 1 root root  4096 Sep 26 03:53 misc
drwxr-xr-x 1 root root  4096 Sep 26 03:56 pkg
-rw-r--r-- 1 root root    26 Sep 26 03:52 robots.txt
drwxr-xr-x 1 root root  4096 Sep 26 03:53 src
drwxr-xr-x 1 root root  4096 Sep 26 03:53 test

添加 PATH 路径

unix_user:~$ cat ~/.profile

# 最後に追記。上の $GOROOT/bin は go 自体の実行のため?
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$HOME/go/bin

确认安装

unix_user:~$ go env GOPATH
/home/unix_user/go

unix_user:~$ go env GOROOT
/usr/local/go

# 環境変数には設定されていない
unix_user:~$ printenv | grep GO
unix_user:~$

试着实际下载外部库。

unix_user:~$ go get -u github.com/motemen/gore/cmd/gore

unix_user:~$ tree ~/go
/home/unix_user/go
├── bin
│   └── gore
└── src
    ├── github.com
    │   ├── mattn
    │   │   └── go-runewidth
    │   │       ├── LICENSE
    │   │       ├── README.mkd
    │   │       ├── benchmark_test.go
    │   │       ├── go.mod
    │   │       ├── runewidth.go
    │   │       ├── runewidth_appengine.go
    │   │       ├── runewidth_js.go
    │   │       ├── runewidth_posix.go
    │   │       ├── runewidth_posix_test.go
    │   │       ├── runewidth_test.go
    │   │       └── runewidth_windows.go
    │   ├── mitchellh
    │   │   └── go-homedir
    │   │       ├── LICENSE
    │   │       ├── README.md
    │   │       ├── go.mod
    │   │       ├── homedir.go
    │   │       └── homedir_test.go
    │   ├── motemen
    │   │   ├── go-quickfix
    │   │   │   ├── LICENSE
    │   │   │   ├── README.adoc
    │   │   │   ├── cmd
    │   │   │   │   └── goquickfix
    │   │   │   │       └── main.go
    │   │   │   ├── quickfix.go
    │   │   │   ├── quickfix_test.go
    │   │   │   └── testdata
    │   │   │       ├── general
    │   │   │       │   └── general.go
    │   │   │       ├── importname
    │   │   │       │   └── importname.go
    │   │   │       ├── rangestmt
    │   │   │       │   └── rangestmt.go
    │   │   │       └── revert
    │   │   │           └── revert.go
    │   │   └── gore
    │   │       ├── LICENSE
    │   │       ├── Makefile
    │   │       ├── README.md
    │   │       ├── cli
    │   │       │   ├── cli.go
    │   │       │   ├── cli_test.go
    │   │       │   └── run.go
    │   │       ├── cmd
    │   │       │   └── gore
    │   │       │       └── main.go
    │   │       ├── command_name.go
    │   │       ├── command_name_test.go
    │   │       ├── commands.go
    │   │       ├── commands_test.go
    │   │       ├── complete.go
    │   │       ├── complete_test.go
    │   │       ├── debug.go
    │   │       ├── doc
    │   │       │   └── screencast.gif
    │   │       ├── errfilter.go
    │   │       ├── errfilter_test.go
    │   │       ├── go.mod
    │   │       ├── go.sum
    │   │       ├── gocode
    │   │       │   ├── gocode.go
    │   │       │   └── gocode_test.go
    │   │       ├── gore.go
    │   │       ├── liner.go
    │   │       ├── log.go
    │   │       ├── node.go
    │   │       ├── node_test.go
    │   │       ├── nodebug.go
    │   │       ├── option.go
    │   │       ├── quickfix.go
    │   │       ├── session.go
    │   │       ├── session_test.go
    │   │       ├── terminal_unix.go
    │   │       ├── terminal_windows.go
    │   │       ├── utils.go
    │   │       └── wercker.yml
    │   └── peterh
    │       └── liner
    │           ├── COPYING
    │           ├── README.md
    │           ├── bsdinput.go
    │

卸载

删除解压后的文件们在GOPATH上。

unix_user:~$ sudo rm -rf /usr/local/go

请确认是否删除安装。

unix_user:~$ go env GOPATH

Command 'go' not found, but can be installed with:

sudo apt install golang-go
sudo apt install gccgo-go

# GOPATH にダウンロードしたライブラリは残っている
unix_user:~$ ls -la ~/go
total 0
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:07 .
drwxr-xr-x 1 unix_user unix_user 4096 Oct 15 18:14 ..
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:07 bin
drwxrwxrwx 1 unix_user unix_user 4096 Oct 15 18:06 src

# ダウンロードしたライブラリとかも手動で消す
unix_user:~$ sudo rm -rf ~/go
unix_user:~$ ls -la ~/go
ls: cannot access '/home/unix_user/go': No such file or directory

公寓怎么样?

版本太老了。。。

unix_user:~$ sudo apt install golang-go
[sudo] password for unix_user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-race-detector-runtime golang-src pkg-config
Suggested packages:
  bzr mercurial subversion
The following NEW packages will be installed:
  golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-go golang-race-detector-runtime golang-src pkg-config
0 upgraded, 7 newly installed, 0 to remove and 243 not upgraded.
Need to get 40.3 MB of archives.
After this operation, 225 MB of additional disk space will be used.
Do you want to continue? [Y/n] N
Abort.
广告
将在 10 秒后关闭
bannerAds