golang

How to Create a Simple Application in Go Language

This article will cover a tutorial on creating a simple “Hello World” application in Go programming language. All code samples and commands in this article are tested with the Go language version 1.14.7 on Ubuntu 20.10.

About Go Language

Go is a relatively new programming language being developed at Google. It is similar to C and C++ in many ways, with some very useful additions that makes writing code and rapid prototyping much simpler and safer. It is a compiled programming language and features statically typed syntax (like C). It also features automatic garbage collection and code written in Go is much more readable than other similar compiled programming languages. In simplest terms, you can think of it as a programming language created by picking up the best features from both C and Python. Go is faster than Python and its speed is comparable to C, even faster in many cases. Go does not provide object oriented programming structure and classes you may have seen in other programming languages. Though there are ways to make methods behave like classes in Go language.

Installing Go Language in Linux

You can install Go programming language in Ubuntu by running the command mentioned below:

$ sudo apt install golang

Go language has been packaged and included in repositories of all major Linux distributions. You can install Go language packages from the default package manager. You can also directly download binaries from the official Go language webpage. Once you have downloaded the tar archive, run the commands specified below in succession to install Go language. Make sure to replace the name in the first command with the name of the archive you have downloaded from the official Go website.

$ tar -C /usr/local -xzf go1.14.7.linux-amd64.tar.gz

$ echo "export PATH=$PATH:/usr/local/go/bin" >> "$HOME/.bashrc"

$ source$HOME/.bashrc”

To verify that Go has been successfully installed on your system and its compiler working properly, use the following command:

$ go version

You should see some output like this:

go version go1.14.7 linux/amd64

Full Code

Full code for a “Hello World” application in Go language is given below.

package main


import "fmt"


func main() {

    fmt.Println("Hello World !!")

}

The same “Hello World” application can be re-written in Go language emulating object oriented patterns:

package main


import "fmt"


type HandleString struct {

    name string

}


func (newString HandleString) print_string(){

    fmt.Println(newString.name)

}


func main() {

        s := HandleString{"Hello World !!"}

        s.print_string()

}

Assuming that any of the code samples above is saved into a file named “helloworld.go”, you can run the command below to execute the code:

$ go run helloworld.go

After executing the above code samples, you should get output like this:

Hello World !!

Step-by-step Explanation

The first statement “package main” is required to create an executable command or binary in Go language. Go source files under the same directory are put together into packages. All variables and functions in these source files can be shared between the specified packages.

Next, “fmt” package is imported so that you can use functions like “Println” in the main code. “Fmt” is a part of the standard library packages in Go language and it provides numerous useful helper functions. It is not mandatory but it is used in almost all programs written in Go language.

Lastly the “main” function prints “Hello World !!” string. The “main” function is automatically called whenever you execute a Go language program.

In the object oriented example, struct is used to define a new “HandleString” type. A struct is a group of data fields and variables. Functions can be attached to structs to handle these data groups. Thus structs provide a neat way to define classes in Go language. A new field “name” of type “string” is declared in the struct.

Next, the function “print_string” is added to the “HandleString” struct. This function has a “newString” argument that acts as “receiver”. This receiver can be used to access fields of a struct instance. For instance, “newString.name” is used to access the name field from “HandleString” struct.

Finally, a new instance of the “HandleString” struct is created and the function “print_string” is called on it to print the “Hello World !!” string.

Both code samples listed above produce the same output.

Compiling a Go Application

In order to compile “Hello World” Go program, you can use “build” command to generate an executable binary:

$ go build helloworld.go

You should now have a “helloworld” executable binary located in the same directory where your main program file is saved.

You can run the executable binary by using the command specified below:

$ ./helloworld

It will produce the same output as the “go run” command.

Conclusion

This tutorial touches only a few basics to create a “Hello World” program in Go language. It should get you started. To create more advanced programs, refer to official documentation.

About the author

Nitesh Kumar

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.