Hello World Go Program

Published by user on

In this chapter, we will write our first Go program to understand the basic building block of a Go program.

Hello World Program

package main

import "fmt"

func main() {
	/** Program to understand basic structure.**/
	fmt.Println("Hello, from Devcubicle!!!")
}

Explanation

package main

Go programs are bundled in packages, so every Go program must have a package declaration as the first statement. Also, the main package is the starting point to run a program. If you try to run a program with any other package name it will result in an error. go run: cannot run non-main package

import “fmt”

This line indicates to include fmt package in our program. It means all the files in fmt package will be included in our program by the Compiler. fmt package has functions to print and scan values.

func main()

Go programs execution starts from main() function. The name of the function has to be main otherwise it will result in the following error.

# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package

Run our Go Program

  • Copy the above code to any text editor.
  • Save the file as hello-world.go
  • Go to the directory where you saved the file
  • Paste go run hello-world.go to the command prompt
  • This should produce output
Hello, from Devcubicle!!!

If you are unable to run the program then ensure that installation steps are correctly followed. Generally, the problem occurs when the PATH variable is missing.

Categories: go