/Learn-Golang

Golang programs for learners

Primary LanguageGo

Introduction

Go (also known as the Golang) is an open-source programming language introduced in 2009 and is distributed under the BSD-style license. It was developed by Robert Griesemer, Rob Pike, and Ken Thompson at Google.
In this project, we’ll be learning Golang from basics. This repository will give you the basic programs with some explanation which will help you to understand the concepts. I tried to keep it simple and easily understandable. In order to start typing and running the code, first we need to setup our environment.
What we need :

  1. Windows 10 Home
  2. Go compiler
  3. Visual Studio Code

I’m assuming you are using Windows operating system, so let’s starts from how we could install the Go compiler on our Windows operating system. Also how we could configure the Visual Studio Code editor to work with the Go programming language.

Installing Go Programming Language

The first thing you need to do is navigate to the golang official site https://golang.org/dl/
This will take you to the downloads page of Go.

Download Golang
Download the installer for Windows operating system and start the installation.
The installation process is quite straightforward however if you face any challenges while installing the language do let me know in the comment section.
Once the installation is complete, open your terminal and run the following command.

$ go version
go version go1.16.5 windows/amd64

Add GOPATH environment variable

GOPATH is used to specify the root of your Go workspace. By default, the workspace located at %USERPROFILE%/go for Windows.
To configure GOPATH follow the steps mentioned below:

  1. Create a new folder in your C drive called a“C:\Users\USER\go”. (in my case)
  2. Open Search box, type environment
  3. Windows Search Box
  4. Click on Edit the system Environment variable , This will open the System Properties
  5. Under Advanced tab, click on Environment Variables button at the right bottom.
  6. System Properties
  7. Now in the Environment Manager dialog click the new button from the System Variables section.
  8. Set the GOPATH as name and C:\Users\USER\go as its value, and hit the OK button.
  9. GOPATH Environment Variable

This will configure the GOPATH on your PC.
To check if the GOPATH has been appropriately configured open run dialog by pressing win + r and type %GOPATH% and hit the OK button. If it takes you to the C:\Users\USER\go directory, It means the configuration is successful.

Installing Visual Studio Code

Visual Studio Code is an open-source lightweight code editor by Microsoft that comes bundled with support for TypeScript, JavaScript, and Node.js. However, you could easily get support for Go Language by installing a handy extension.

Install Visual Studio Code editor, open the link https://code.visualstudio.com/Download
After the installation process is complete launch the editor and open the extension manager by pressing Ctrl + Shift + x.
In the extension manager, search box type go and hit enter.

Visual Studio Golang Extention
In the search results, you’ll find the Go extension by the GO team at Google. Hit the install button and let the installation process complete.

After installation, open the command palette by pressing Ctrl + Shift + p and run the Go: Install/Update Tools command.

Visual Studio Install/Update Tools

This will present you with a list of tools you need to install.

Visual Studio more Tools

Install everything listed in the dropdown.

Let’s write some code.
Once the installation process is complete create a new hello.go file in your vscode editor and add the following hello world program to the file.

package main
import  "fmt"
func main() {
    fmt.Println("Hello world...")
}

Now let’s test the autocomplete feature by deleting Println(“Hello world…”) and hitting Ctrl + Space after fmt.

Visual Studio Go Editor
You could also try running the program by using the following command in the terminal.

$ go run hello.go
hello world...

And that’s it!
So, let’s get started with Programming now.