Go is an extremely powerful language to have in your toolbelt as a developer on any level. Go's goal is to create lightweight executables without the hassles, or drawbacks other programming languages provide. It deals with garbage collection(which coming from C is a very nice touch). Aside from garbage collection go was designed to utilize modern computer cpus, allowing us to write code that can be executed and run faster.
- Overview
- Table of Contents
- Repo Goal
- Download
- Installation
- Getting Started
- Topics
- Conclusion
- Extra Sources
The goal of this repository is to give any viewers the information they need to start writing go code. I will also personally use it as a reference if I ever need a refresher on syntax or general usage. Many of the topics covered are also covered in the Effective Go documentation. A list of the topics covered can be found under the Topics section.
The above link will bring you to the downloads page for Go, in the section below I will highlight some common OS installations such as Linux, Windows and Mac. Make sure the version you download is compatible with your OS.
Please note, this is for common distributions of linux such as Ubuntu, using the linux precompiled installer
For more installation on installing Go from the source, please refer to their documentation https://golang.org/doc/install/source
-
Extract the information within the downloaded archive file downloaded from the Go website, to /usr/local this should look like
go[version #].linux-amd64.tar.gz
Important: Files stored in the location /usr/local/go will need to be removed or overwritten if they exist to ensure the installation has no errors. To ensure the location is cleared and no files are there we will remove that file and then extract the tar.gz file
* Note that in this case I am using the Go version 1.16.3 tar.gz at the time of creating this, so the file name may need to be replaced if you copy and paste this snippet
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.16.3.linux-amd64.tar.gz
-
Now that the Go files are in the local directory we need to add the PATH environment variable. To do this open either $HOME/.profile (~/.profile) or /etc/profile in a text editor of your choice and append the following line to the end of the file.
export PATH=$PATH:/usr/local/go/bin
Due to the method of adding go to the path, the changes will not take effect until a reboot, to have them take effect immediately run the export command from the command line.
-
Ensure Go is installed and referenced correctly in the PATH by getting the go version with the command below.
go version
This should return something along the lines of "go version go1.16.3 linux/amd64"
The windows installation process is fairly simple. In order to get started make sure you have the .msi file, available from the link under the "Download" section.
-
Open the MSI file, this will open up a generic windows installation window. The default location of install will be in the C:\Windows\Program Files (x86) or C:\Windows\Program Files directory. You can change that if needed. Please note that any open command prompts will need to be closed and reopened for environment variables to update.
-
To ensure the installation went smoothly, go into a Command Prompt Window and run the following command.
go version
If go is installed correctly the correct version should be printed in the cmd window.
The Mac install of go does essentially the same thing as the linux install, however it is automated for the most part.
-
The Mac download file from the go website should be of type .pkg, Once it is download open the package file and follow the prompts that appear, just like in linux the install should end up in the /usr/local/go directory and the installer should add the environment path to the same location.
-
To ensure the install worked open up a command prompt and enter the following command
go version
This should return the current version of go installed.
The end goal for this repository is to build your skills up to be able to write testable Go packages, and modules, that can be shared on a version control site such as github. To start off however creating a full blown package is not needed. Examples shown in directories 0x00-0x07 were created and run using the go run
command, more on that below.
Modules in Go are usually stored in one folder, to keep everything it will rely on in one place. If you are following along with each directory below is how you would do such without creating modules. For this example I will be using the 00_Hello_World directory.
Let's say you are following along and make your way into the 00_Hello_World directory, and see our first file containing code. How do we run it?
We would want to start by creating a location on our local machine for the file to run. If you are on linux you can do that with the mkdir command, I will use a folder named "project". Once we have that folder created, navigate inside of it.
Our new location should be
project/
Once inside of this directory we are inside of our "environment", when you get to the modules section you will see how directories can be turned into modules. For now this will do just fine. In this directory we can have as many of the example files as we want. Just be sure to specify the file name when running or compiling the scripts as seen below.
Once we have the environment setup we can start focusing on running the code we write. Let's take a look at running the "00_Hello_World.go'' script, inside the "0x00_Hello_World" directory.
Go is an interpreted and compiled language, this means we can either run the go script through an interpreter written in Go, or we can compile the code into a binary and execute the file.
Let's start with running a script.
To run a simple Go script, like many in this repository, we will make use of the go run [Path/to/file]
command. This command will tell Go to start an interpreter and execute the script passed in. This is super useful when it comes to testing changes, or finding bugs within a script.
For more information on this command check out the link below
https://golang.org/cmd/go/#hdr-Compile_and_run_Go_program
Compiling the script will create an executable file. On windows this will generate an EXE and on linux and mac it will generate an ELF file. To compile the script we will use a command close to running however the second argument goes from run to build.
go build [path/to/file]
This will create and place a new executable with the same file name as the script in the current directory.
Below is a list of topics covered in this repository. If you are using this repo as a means to learn Go it's recommended you start at the top and work your way down(0x00-0x10). This list is here because it will be an easy way to navigate to a specific topic without browsing for them.
- Printing
- Comments
- Variables
- Control Structures
- Functions
- Return
- Datatypes
- Composite Literals
- Init Function
- Methods
- Interfaces
- Errors
- Unit Tests
- Concurrency
Go is a language that can be vital to giving you the edge over your competitors. The syntax is relatively forgiving and straightforward. While the package management can take a little more time to get used to then say javascript or python that can utilize npm or pip, it can be more powerful when it comes to the share-ability of code. There's a boat load of reasons Go is amazing, but I think the best way for you to find out is to get to writing some code.