本文详细介绍了Windows10子系统Linux(WSL)的安装和配合Goland、vscode的使用方法,个人感觉WSL使得后台开发变得很方便,特此分享出来。原文章位于github中维护的learn-golang项目中
WSL(Windows Subsystem for Linux)安装与使用
官方介绍:WSL(Windows Subsystem for Linux)是适用于Linux 的 Windows 子系统,是一个为在Windows 10和Windows Server 2019上能够原生运行Linux二进制可执行文件的兼容层。
直白的讲,就是借助WSL可以在Windows10中直接编译出Linux环境的可执行文件,让Windows下的开发有机会摆脱庞大的虚拟机,有点mac下开发的感觉,特别适合我这种没有mac的贫困码农。
打开子系统功能
在win10 power shell输入如下指令:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
win10 安装Ubuntu
从win10应用商店安装Ubuntu:
![](https://static.studygolang.com/190731/01224ca58f5f4ddf06ff2cab90ae0908.jpg)
安装后win10桌面会提示设置用户名和密码:
![](https://static.studygolang.com/190731/62c20e63d0ea5b9b8107cf54abeec1d4.png)
GO环境配置
GOROOT配置
配置共享目录
为了方便的在Windows和WSL之间拷贝文件,要先设置共享目录。
WSL中的/mnt/c/Users/HideOnBush/
路径为Windows10在Linux下的挂载分区,为方便和win10共享文件,在Windows中新建UbuntuShare文件夹,在WSL添加软链接:
ln -s /mnt/c/Users/HideOnBush/UbuntuShare/ win10
HideOnBush是我的Windows中的用户名,需要替换为你实际的Windows用户名哦。
WSL安装Go
从go语言中文网下载Linux go安装包(我使用的是1.12.5),放到刚才设置的共享目录中,就可以在WSL中操作了。
解压到/usr/local:
cd win10
sudo tar -xzf go1.12.5.linux-amd64.tar.gz -C /usr/local/
GOPATH配置
将Windows和Linux的GOPATH设置为同一个路径,就可以实现在win10写代码时和在Linux编代码共用一套第三方包。
查看win10环境变量
![](https://static.studygolang.com/190731/e3c2af3f5d1ad771a834b9e962e1253c.png)
![](https://static.studygolang.com/190731/a79d9cda4f3061a2f43a7a50c6bf39d8.png)
可以看到我的Windows中GOPATH路径为
Users/HideOnBush/go
,接下来将WSL中的GOPATH设置为同一路径,完成后续操作就可以方便的在Windows中编写代码并调用Ubuntu编译代码了。
设置wsl GOPATH:
vim ~/.bashrc
末尾添加:
export GOPATH=/mnt/c/Users/HideOnBush/go
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
使修改立即生效:
source .bashrc
使用go env
查看GOPATH是否配置成功:
![](https://static.studygolang.com/190731/758676ea24e06782cbe5d61d263f9b6c.png)
可以看到我的GOPATH已经设置为和Windows中的GOPATH一致了。
更新软件源
sudo cp /etc/apt/sources.list /etc/apt/sources.list_origin
sudo vim /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
sudo apt-get update //更新源
sudo apt-get upgrade //根据软件源的信息更新软件
在Windows中调用WSL bash
接下来根据你使用的编译器,将编译器使用的bash修改为WSL内的bash,就可以在Windows下编译出Linux下的可执行文件。
配合goland 使用
修改bash为wsl所在路径
找到wsl命令行所在路径,
![](https://static.studygolang.com/190731/f0a530a334fa308a959c9f20026a3fc5.jpg)
设置goland "shell path",
![](https://static.studygolang.com/190731/fbc5f031f1d6bac4acedd58eb2dd7f61.jpg)
修改goland换行符为UINX下的LF:
![](https://static.studygolang.com/190731/86f362067c050a978d1aea7e994e0875.jpg)
terminal测试,
![](https://static.studygolang.com/190731/4d978a83cb6945de056c2e703b70f3a6.jpg)
不能直接使用goland的run图标运行,因为goland默认使用的是Windows中的编译环境,必须要在terminal中编译才是使用的WSL的bash编译。
配合vs code使用
安装wsl后在vscode terminal可以看到wsl(windows system linux)的窗口,就可以在这儿方便操作Linux。
![](https://static.studygolang.com/190731/09880d57b9b4a51f4d386c66d80beea5.jpg)
为了防止和Linux内编写的文件换行符有冲突,推荐修改换行符为Linux样式,如下:
![](https://static.studygolang.com/190731/38cb6819c099032840f3a12301b01fd4.jpg)
至此,Windows10的Linux子系统的开发环境已经基本搭建完成,后续根据你的需要在WSL安装对应工具即可,如果觉得WSL的命令行太难用,可以使用Goland的terminal,或者使用Secure CRT等SSH工具。
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)