Go Hello World

In this tutorial, you will learn the popular example of every programming language i.e. Hello World program.


Hello World program is considered the entry point when you are learning any new language. This provides the basic structure of the programming. Without much delay let’s check the example of the Hello World program.

Hello World in Go

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

Output

Hello World

Explanation

Now let’s decode each of the lines mentions in the above program.

  • package main – The first line of the program is package declaration. Here the package name is main. The package provides the reusability of the code. Generally, the main function resides in this package. So, it is necessary to use  package main before using the main function.
  • import “fmt” – The next line is import package, here we are importing the "fmt" package. The compiler will include the "fmt" package during the execution of the program. This is required to print the output in the standard output device.
  • func main() – This is the entry point of the Go program. The main is a special function in Go. The { and } braces indicate the start and end of the main function.
  • fmt.Println(“Hello World”) – The Println function belongs to the fmt package and used to write the message to the standard output device. The standard procedure to call a function in Go language is package.function().

How to run Go program

To run the above program just create a file with .go extension using your favorite text editor in your workplace. In my case, I am saving the file as Hello.go in my workplace i.e. at D:\Code\Go.

Now open the command prompt or Powershell and browse to the Go workplace.  After that enter the following command:

go run Hello.go

Output

Hello World

go run is used to execute any Go program, Here go run Hello.go executes our program and provides desired output.

Please get connected & share!

Categories

Advertisement