Go for Go | Skcript

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

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.

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>

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.")

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.goorgo 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.

Comments and Discussions Please enable JavaScript to view the comments.

This is a companion discussion topic for the original entry at https://www.skcript.com/svr/go-for-go/

Do you need to execute tasks with percision and accuracy?

Grow and scale your organization easily by automating business crucial functions with Robotic Process Automation (RPA).