Go for Go

On , by

  • goforgo Series (2 Part Series)
  • Go for Go Protobuf with 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?#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:#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

    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:#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:#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.

  • goforgo Series (2 Part Series)
  • Go for Go Protobuf with GO
    Share this article on Twitter or LinkedIn
    Previous Blog Post: Ownership & Borrowing in Rust #RustWorthy Part 3
    Subscribe to Sudo vs Root

    Our newsletter rolls out every month. No fluff. Pure content.

    Comments and Discussions
    Skcript https://www.skcript.com/svr/go-for-go/ https://www.skcript.com/svrmedia/heroes/go-for-go@1.5x.jpg