Go Hello World Program

When you start to learn a new programming language you should start with the most basic and famous Hello World program. Before we write the hello world program, we need to know about the anatomy of a Go program.

Anatomy of a Go Program

Every Go program must contain the name of the package it belongs to, and the declaration of the package is usually the first line of the Go program. We declare the package like this:

package main

It should be noted that name of the package can be anything, I just wrote main as it is the most basic name you can provide for a Go file.

After the package name usually comes the imports. Imports mainly include the third-party packages or the standard package code names from which we import some methods or structures to write the application. The import declarations look something like this:

import "fmt" // import followed by the name of the package

In the example above, we imported the famous fmt package of the Go standard library. We will use this package in our Hello World example also.

Lastly, we have the main function. The main function looks like this:

func main(){
   // code inside
}

It should be noted that the main function is the entry point of every Go application. It is the function that the Go runtime looks for.

Hello World Program in Go

A Hello World program in Go will look something like this. It includes a package name, import statement, and a main() function to print the Hello World message to the console.

package main
import (
	"fmt"
)
func main() {
	fmt.Println("Hello World")
}


Hello World

Conclusion

In the above article, we learned about the anatomy of a Go program, and then we saw the classic Hello World program in Go. In our next tutorial, we will learn about variables and data types in Go.