Go - Program Structure


Before we study the basic building blocks of Go programming language, let us first discuss the bare minimum structure of Go programs so that we can take it as a reference in subsequent chapters.

Hello World Example

A Go program basically consists of the following parts −

Let us look at a simple code that would print the words "Hello World" −

Live Demo
package main

import "fmt"

func main() {
   /* This is my first sample program. */
   fmt.Println("Hello, World!")
}

Let us take a look at the various parts of the above program −

Executing a Go Program

Let us discuss how to save the source code in a file, compile it, and finally execute the program. Please follow the steps given below −

$ go run hello.go
Hello, World!

Make sure the Go compiler is in your path and that you are running it in the directory containing the source file hello.go.