Skcript Technologies Private Limited

← All Articles

Go for Go

— Written by

Go for Go

Among the number of programming languages out there, Go or Go language is gaining traction in recent times for numerous development tasks. If you want to develop distributed networked services or cloud-native applications, Go lang will be the best choice. Start with this blog. This article explains the basics of Go Language from setting up workspace to executing a simple Go program.

I started to learn Go a few weeks ago as a hobby. In this article series, I will document everything I learn.

What is GO?

As all probably know, Go is a general-purpose language developed by Google and has a wide range of applications. Go takes us back to the syntax of C++.

The most important feature of Go is its concurrency. It allows multiple processes to run simultaneously and effectively thanks to its garbage collector, goroutines (a lightweight thread managed by Go for efficient concurrency) and channels. And of course, GO is an open-source language.

So, let’s now dive into the workspace setup.

Setting Up Workspace:

The go program is made up of packages. The package acts as an entry point for an application. Before you start crushing it on Go, we need to set up a workspace. A normal Go workspace looks like this:

    hello                          # command executable
    outyet                         # command executable
        .git/                      # Git repository metadata
	    hello.go               # command source
	    main.go                # command source
	    main_test.go           # test source
	    reverse.go             # package source
	    reverse_test.go        # test source
        .git/                      # Git repository metadata
	    reader.go              # package source
	    writer.go              # package source

A typical workspace contains many source repositories containing many packages and commands. Most Go programmers keep all their Go source code and dependencies in a single workspace.

The tree above shows a workspace containing two repositories (example and image). The example repository contains two commands (hello and outyet) and one library (stringutil). The image repository contains the bmp package and several others.

The GOPATH environment variable specifies the location of your workspace. Set the GOPATH variable to your desired directory by using the command below:

export GOPATH = <desired directory>

A simple Go program:

We can now dive into writing a simple Go program. I have set my Go workspace as shown below. My GOPATH variable is set to my “go_workspace”.

I have created the package folder “simple_program” in the src folder under the corresponding repository. Let’s now write a simple go program in “simple_program/simple_program.go

package main

import "fmt"

func main() {
	fmt.Println("This is a simple go program.")

Executing a GO program:

As said earlier, package main acts as the entry point. There are two ways you can run a go program

  1. Build as an executable and run. To build the Go program as an executable in the program directory run the command - go build file_name.go or go install file_name.go

    The Go build command creates an executable file in the current directory which can be executed as below:

    The Go install command - created an executable file in the bin folder of the GOPATH and this bin can be added to our environment PATH and can be used anywhere.

  2. The simple way to run is by using the command - go run file_name.go.

That’s it for now guys. Will meet you guys again with another interesting topic.

Up next

Ownership & Borrowing in Rust #RustWorthy Part 3
Skcript /svr/go-for-go/ /svrmedia/heroes/go-for-go@1.5x.jpg
Skcript Technologies Private Limited

Book a free consultation

Book a time with our consultants to discuss your project and get a free quote. No strings attached.