DEV Community

Cover image for GO for GO
Heertheeswaran V
Heertheeswaran V

Posted on • Edited on • Originally published at skcript.com

GO for GO

#go

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/
    github.com/golang/example/
        .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
    golang.org/x/image/
        .git/                      # Git repository metadata
    bmp/
        reader.go              # package source
        writer.go              # package source
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

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.")
}
Enter fullscreen mode Exit fullscreen mode

Executing a GO program:

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

  • 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: Alt Text
    • 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. Alt Text
  • The simple way to run is by using the command - go run file_name.go.

Alt Text

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

Top comments (0)