By now you must be itching to write and execute your first Go program and in this blog, we will help you do that. However our first Go Program will not be as simple as a Hello World program, but it will use all the features of Go that will we have learnt till now as part of this Go tutorial series.

This blog is part of the tutorial series – Learn Go, in which we will learn about Go language step by step.

Table of Contents

Note: Before we go further, one important point to note here is that Go does not require us to terminate any statement with semicolon ( ; ) , as some languages such as Java requires us to do.

1.0 Anatomy of Function in Go

Before we write our first Go Program, lets take a very brief look at what a function is in Go and how does it looks like.


  • is a block of executable code
  • can be executed/called from other places in our code
  • can pass arguments to function (if supported)
  • can return values to calling code, after its execution.

A function definition looks like

func [function-name] ( [function-parameters] )  {

Note: Function Parameters and Return Types are Optional.

Following is an example of a simple function (with no arguments and no return types) – just prints a String message

func printMessage() {
   fmt.Println("Hello World")

We will look at functions in more details in a later blog – Functions in Go.

2.0 Anatomy of a Go Program

A simple go program looks like

package [package-name]

import [packages]

[Main function declaration]

A go program can contain many packages, many Go files and multiple functions.
However let’s not go into that complexity now, and look at the simplest executable go program.

Generally a Go Program starts execution from a main() function – which is special function defined in a package named as main

So a simple executable Go program, that prints “Hello World” message would look like

package main

import "fmt"

func main () {
   fmt.Println("Hello World!")

Above Go code when executed, would just print a message – Hello World

3.0 First Go Program

In this section we will write our first go program and use all the concepts that we have learned so far in the Learn Go tutorial series.

  • package declaration
  • importing packages
  • factored declaration of constants and variables
  • short hand initialization of variables
  • main function
  • Using Println and Printf

Below is our first program that makes use of all the above concepts

// declaring the package name
package main

// Importing packages
import "fmt"

// Factored variable declaration
var (
   n1, n2 = 5, 10
   s1, s2 = "aaa", "bbb"

// Constant declaration
const (
   WELCOME_MESSAGE = "Hello, This is our first Program"

// Main function
func main () {

   // Variable declaration using short hand initialization
   n3, n4 := 20, 25

   // Using Println

   // Using Printf
   fmt.Printf("Printing Numbers n1=%d, n3=%d, n4=%d n", n1, n3, n4)

   // Using Printf
   fmt.Printf("Printing Strings s1=%s n", s1)


Save the above file as hello.go, and to execute run the following command on terminal go run hello.go

Once you execute the above program, it will print something like this.

Hello, This is our first Program
Printing Numbers n1=5, n3=20, n4=25 
Printing Strings s1=aaa


Please feel free change the above program, and try out all the concepts learned so far before we move to next sections.

Top ∆

In the next blog of the Learn Go tutorial series, we will cover Loops, If-Then-Else and Switches in Go