dev | 2019-10-14T00:00:00+05:30

Go for Go

Hellonext, Inc.

Heertheeswaran V

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:

bin/ hello # command executable outyet # command executable src/ .git/ # Git repository metadata hello/ hello.go # command source outyet/ main.go # command source main_test.go # test source stringutil/ reverse.go # package source reverse_test.go # test source .git/ # Git repository metadata bmp/ 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.

Last updated: September 7th, 2023 at 7:42:06 AM GMT+0

Hellonext Blogpost Author Profile

Heertheeswaran V

Heerthees is the Head of Engineering at Hellonext. He writes about how we engineer Hellonext to be the best user feedback tool in the market.