Learn Concurrency in Go by Building a Download Manager Part-1
How to setup golang using the version manager
Overview
Concurrency is a crucial feature of modern programming languages and frameworks, enabling developers to write efficient, scalable, and responsive applications. Go is a language that embraces concurrency as a first-class citizen, providing built-in support for lightweight threads called goroutines and message passing via channels. In this tutorial, we'll explore using these concurrency primitives to build a download manager in Go.
A download manager is a program that helps you download files from the internet, allowing you to pause and resume downloads, schedule them for later, and handle errors and retries. Building a download manager is a great way to learn about concurrency in Go, as it involves coordinating multiple concurrent tasks such as downloading data, writing files, and managing the state.
Throughout this tutorial, we'll cover the basics of concurrency in Go, including how to create and synchronize goroutines, use channels for communication and synchronization, and handle errors and timeouts. We'll also explore advanced topics such as managing resource limits and implementing a user-friendly CLI interface.
By the end of this tutorial, you'll have a solid understanding of concurrency in Go and how to apply it to real-world problems. So let's get started and build our own download manager in Go!
Setup GoLang For Development
The first thing to do is to install Golang on your local machine. You can follow the installation procedure on the official website. But also I will be showing the installation process for arch linux.
sudo pacman -S go
From the command above latest version of go will be installed in your system. But I prefer using a version manager, as I sometimes check out go repos on GitHub, and often I found that older versions of golang. So I will be using goenv which I will recommend to every go developer to use, and it's easy to set up.
Just follow the instruction below or read about it on INSTALL.md
To install goenv, clone the repository to your preferred location. You can use the command below to clone the repository to $HOME/.goenv
:
git clone https://github.com/syndbg/goenv.git ~/.goenv
Once you have cloned the repository, set the environment variable GOENV_ROOT
to point to the path where the repository is located. Also, add $GOENV_ROOT/bin
to your $PATH
so that you can access the goenv command-line utility. To do this, you can add the following lines to your ~/.zshrc
file:
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
After setting the environment variable and updating your $PATH
, you need to enable shims, manage your GOPATH
and GOROOT
, and activate auto-completion. Add the following line to your ~/.zshrc
file, making sure that it is placed at the end of the file since it modifies the $PATH during initialization:
eval "$(goenv init -)"
Finally, you should add GOPATH
and GOROOT
to your shell. You can add the following lines to your ~/.zshrc
file after eval "$(goenv init -)"
:
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"
After completing these steps, you can use goenv to manage your Go versions. Now, restart your shell so the path changes make take effect:
exec $SHELL
Install the go version of your liking:
goenv install 1.12.0
To check go setup properly,
go version
Conclusion
We now know how to install and switch between different versions of Go. Next part I will go into the file structure.