How to Write a Simple Application in Go Language

In this article, we’ll explain how to write a simple application in Go language.

Go language 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.

Here is guide to install Go language in Ubuntu 20.04

We can write our first Hello World! program using following steps:

1. Create a folder and navigate into it.

# mkdir hello

# cd hello

2. Create a Go module using the go mod command.

# go mod init hello

3. Create a file named hello.go

# touch hello.go

4. Edit the file hello.go

# vi hello.go

5. Add following lines

package main

import “fmt”

func main() {
fmt.Printf(“Hello World!”)

Execute our first Go program using following command:

# go run hello.go


Hello World!

Compiling a Go Application

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

# go build hello.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:

# ./hello

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

In this article, we have seen how to write a simple application in Go language.

Get a high performance dual E5 series dedicated server and cheap KVM VPS.

Related Articles