How to Download and Use Go in WSL
Are you a Windows user who wants to learn and use Go, the popular programming language that powers many modern applications? Do you want to enjoy the benefits of a Linux environment without having to dual-boot or use a virtual machine? If so, this article is for you.
In this article, you will learn how to download and use Go in WSL, the Windows Subsystem for Linux. WSL is a tool that lets you run Linux applications and utilities directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup. You will also learn how to use VS Code and GoLand, two popular IDEs for Go development, with WSL.
download go in wsl
By the end of this article, you will be able to write, run, debug, and test Go code in WSL, using your favorite Linux distribution and IDE. Let's get started!
How to Install WSL on Windows
Before you can install and use Go in WSL, you need to install WSL on your Windows machine. WSL is an optional Windows feature that you can turn on easily. Here are the prerequisites and steps to install WSL:
Prerequisites
You need a Windows 10 or 11 machine with an x64 processor.
You need at least 4 GB of RAM and 10 GB of free disk space.
You need an internet connection to download Linux distributions from the Microsoft Store.
Steps to enable WSL feature
Open PowerShell as an administrator from the Start Menu.
Type or paste the following command and press Enter:Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Wait for the command to finish and restart your machine when prompted.
Steps to install a Linux distribution from the Microsoft Store
Open the Microsoft Store app from the Start Menu.
Search for "WSL" and select the Linux distribution that you want to install. You can choose from Ubuntu, Debian, Kali, OpenSUSE, Arch Linux, etc.
Click on the "Get" or "Install" button and wait for the installation to complete.
Launch the Linux distribution app from the Start Menu or the Microsoft Store.
Set up your username and password for the Linux instance. These are just for Linux and don't need to match your Windows credentials.
Congratulations! You have successfully installed WSL on your Windows machine. You can now run Linux commands and applications from the Linux terminal. You can also access your Windows files from Linux using the /mnt/c/ path.
How to Install Go in WSL
Now that you have WSL installed, you can install Go in your Linux instance. There are different ways to install Go in Linux, but we will use the method of downloading and extracting the Go binary from the official website. Here are the steps to install Go in WSL:
How to install Golang on WSL/WSL2
How to setup Go development environment on WSL 2
Download and install Go for Windows Subsystem for Linux
How to run Go programs on WSL 2
How to update Go version on WSL/WSL2
How to configure Go path and root on WSL 2
How to use Visual Studio Code with Go and WSL 2
How to install Go modules on WSL/WSL2
How to test and debug Go code on WSL 2
How to use Go tools like gofmt and golint on WSL/WSL2
How to install and use Docker with Go and WSL 2
How to build and deploy Go applications on WSL 2
How to use Git with Go and WSL 2
How to install and use Kubernetes with Go and WSL 2
How to use Go web frameworks like Gin and Echo on WSL 2
How to install and use PostgreSQL with Go and WSL 2
How to use Go database drivers like pq and sqlx on WSL/WSL2
How to use Go testing frameworks like testify and ginkgo on WSL 2
How to use Go mocking libraries like gomock and mockery on WSL/WSL2
How to use Go logging libraries like zap and logrus on WSL 2
How to install and use MongoDB with Go and WSL 2
How to use Go data structures like slices and maps on WSL/WSL2
How to use Go concurrency patterns like channels and goroutines on WSL 2
How to use Go error handling techniques like defer and panic on WSL/WSL2
How to use Go interfaces and polymorphism on WSL 2
How to install and use Redis with Go and WSL 2
How to use Go caching libraries like go-cache and bigcache on WSL/WSL2
How to use Go messaging libraries like nats and kafka-go on WSL 2
How to use Go microservices frameworks like go-kit and go-micro on WSL/WSL2
How to use Go gRPC libraries like grpc-go and protoc-gen-go on WSL 2
How to install and use RabbitMQ with Go and WSL 2
How to use Go AMQP libraries like amqp and streadway/amqp on WSL/WSL2
How to use Go RESTful libraries like resty and gorequest on WSL 2
How to use Go GraphQL libraries like gqlgen and graphql-go on WSL/WSL2
How to install and use MySQL with Go and WSL 2
How to use Go ORM libraries like gorm and xorm on WSL/WSL2
How to use Go validation libraries like validator and ozzo-validation on WSL 2
How to use Go configuration libraries like viper and envconfig on WSL/WSL2
How to install and use Elasticsearch with Go and WSL 2
How to use Go search libraries like bleve and elastic on WSL/WSL2
Steps to download and extract Go binary
Open your Linux terminal from the Start Menu or the Microsoft Store.
Go to and check the latest version of Go for Linux. At the time of writing this article, the latest version is go1.17.3.linux-amd64.tar.gz.
Copy the download link of the Go binary and use the wget command to download it in your Linux terminal. For example:wget
Use the tar command to extract the Go binary to the /usr/local directory. You may need to use sudo to get the permission. For example:sudo tar -C /usr/local -xzf go1.17.3.linux-amd64.tar.gz
Steps to edit .bashrc file and set environment variables
Use the nano or vi editor to open the .bashrc file in your home directory. For example:nano /.bashrc
Add the following lines at the end of the file to set the GOROOT and GOPATH environment variables and add them to the PATH variable:export GOROOT=/usr/local/goexport GOPATH=$HOME/goexport PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save and close the file.
Source the .bashrc file to apply the changes. For example:source /.bashrc
Steps to verify Go installation
Use the go version command to check the version of Go that you have installed. You should see something like this:go version go1.17.3 linux/amd64
Use the go env command to check the values of the Go environment variables that you have set. You should see something like this:GOROOT="/usr/local/go"GOPATH="/home/username/go"...
Congratulations! You have successfully installed Go in WSL. You can now write and run Go code in your Linux terminal.
How to Use Go in WSL
Now that you have Go installed in WSL, you can start using it to create and run Go programs. You can also use VS Code and GoLand, two popular IDEs for Go development, with WSL. Here are some tips on how to use Go in WSL:
How to create and run a simple Go program
Create a directory for your Go project in your home directory. For example:mkdir hello-go
Navigate to the directory and create a file named main.go with the nano or vi editor. For example:nano main.go
Type or paste the following code in the file, which is a simple program that prints "Hello, world!" to the standard output:package mainimport "fmt"func main() fmt.Println("Hello, world!")
Save and close the file.
Use the go run command to compile and run the program. You should see something like this:$ go run main.goHello, world!
You have just created and run your first Go program in WSL!
How to use VS Code and GoLand with WSL
If you prefer to use an IDE for Go development, you can use VS Code or GoLand with WSL. Both IDEs support WSL integration and offer features such as syntax highlighting, code completion, debugging, testing, refactoring, etc.
To use VS Code with WSL, you need to install VS Code on Windows and install the Remote - WSL extension from the Extensions Marketplace. Then, you can open your Linux terminal from VS Code by clicking on the green icon in the lower left corner of VS Code and selecting "New WSL Window". You can also open a specific folder or file in WSL by right-clicking on it in File Explorer and selecting "Open with Code".
To use GoLand with WSL, you need to install GoLand on Windows and configure it to use WSL as a toolchain. Then, you can create a new project or open an existing one in WSL by selecting "WSL" as the location type in the New Project or Open dialog box. You can also use the Terminal tool window in GoLand to access your Linux terminal.
How to debug and test Go code in WSLOne of the advantages of using Go in WSL is that you can debug and test your Go code in the same environment as you run it. You can use the built-in tools of Go or the features of your IDE to debug and test your Go code in WSL.
How to debug Go code in WSL
To debug Go code in WSL, you can use the go tool command with the -gcflags option to enable debugging information and the -exec option to specify the WSL executable. For example:go tool compile -gcflags "-N -l" -exec "wsl.exe" main.go
This will compile your main.go file with debugging information and execute it in WSL. You can then use the go tool pprof command to analyze the performance of your program or the go tool objdump command to inspect the assembly code.
If you are using VS Code or GoLand, you can also use their built-in debuggers to debug your Go code in WSL. You just need to configure your launch.json or run/debug configuration file to use WSL as the target platform and specify the path to your Go binary in WSL. You can then set breakpoints, watch variables, evaluate expressions, etc. in your IDE.
How to test Go code in WSL
To test Go code in WSL, you can use the go test command with the -exec option to specify the WSL executable. For example:go test -exec "wsl.exe" ./...
This will run all the tests in your current directory and its subdirectories in WSL. You can also use flags such as -v, -cover, -bench, etc. to customize your testing options.
If you are using VS Code or GoLand, you can also use their built-in testing tools to test your Go code in WSL. You just need to configure your settings.json or run/debug configuration file to use WSL as the target platform and specify the path to your Go binary in WSL. You can then run, debug, or profile your tests in your IDE.
Conclusion
In this article, you have learned how to download and use Go in WSL, the Windows Subsystem for Linux. You have also learned how to use VS Code and GoLand, two popular IDEs for Go development, with WSL. You have also learned how to debug and test your Go code in WSL.
By using Go in WSL, you can enjoy the benefits of both Windows and Linux environments without having to dual-boot or use a virtual machine. You can also leverage the features and tools of your favorite IDEs for a productive and enjoyable Go development experience.
If you want to learn more about Go, WSL, VS Code, or GoLand, you can check out the following resources:
: The official website of Go, where you can find documentation, tutorials, blog posts, etc.
: The official documentation of WSL, where you can find installation guides, troubleshooting tips, FAQs, etc.
: The official documentation of VS Code for Go development, where you can find how to set up VS Code for Go, how to use its features and extensions, etc.
: The official documentation of GoLand for Go development, where you can find how to set up GoLand for Go, how to use its features and tools, etc.
We hope that this article has helped you learn how to download and use Go in WSL. If you have any questions or feedback, please feel free to leave a comment below. Happy coding!
FAQs
What is the difference between WSL 1 and WSL 2?WSL 1 is the original version of WSL that uses a translation layer to run Linux system calls on Windows. WSL 2 is a newer version of WSL that uses a lightweight virtual machine with a full Linux kernel. WSL 2 offers better performance and compatibility than WSL 1 but requires more disk space and memory.
How do I switch between different Linux distributions in WSL?You can switch between different Linux distributions in WSL by using the wsl command with the -d option and specifying the name of the distribution. For example:wsl -d Ubuntu
How do I update Go to the latest version in WSL?You can update Go to the latest version in WSL by following the same steps as installing Go, but using the download link of the latest Go binary. You may need to remove the old Go binary from the /usr/local directory before extracting the new one.
How do I access the Windows file system from WSL?You can access the Windows file system from WSL by using the /mnt/c/ path, which corresponds to the C: drive in Windows. You can also use other drive letters such as /mnt/d/, /mnt/e/, etc. to access other Windows partitions.
How do I access the Linux file system from Windows?You can access the Linux file system from Windows by using the \\wsl$ network path, which shows all the Linux distributions installed in WSL. You can also use the name of the distribution after the network path, such as \\wsl$\Ubuntu, to access a specific Linux instance.
44f88ac181
Comments