Hello World program in Golang

Hello world! is the first program that one writes while learning a language. So, let’s write one using the Go language.

You can open https://play.golang.org/, delete the code in the editing area, and write the below code.

package main

import "fmt"

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

Now, click on the run button, and you will see the output

Hello World!

Let’s look at different parts of the code

Usually, every Go file is divided into three sections

  • The package clause
  • import statements
  • The actual code

And our Hello world program is also divided into these three sections, as shown below.

Hello world program in go

package clause – Every Go file will start with a package clause. A package is a collection of code that does all similar things. The package clause gives the file’s package name in which we are writing the code. We used a special package main to run the code directly ( either through the terminal or through any online IDE).

import statement – import statement is used to import the packages required to perform various tasks like formatting strings or drawing images. Every Go file almost always has one or more import statements. Your code will compile a little faster if we only import the packages we need in our program. We have only imported the fmt package because we have to use its Println function in our program. But what will happen if we import an unused package? We are going to have a look at it later in our post.

The actual code – Now, the remaining part of the file is the actual code. This part is commonly divided into one or more functions. When a Go program runs, it looks for the main function and starts executing it. You can see the main function in our code too.

Note: You might have noticed that we haven’t used semicolons to end our line, like in most languages. In Go, it’s up to you whether you want to use semicolons or not at the end of the statement.

How to run Go programs using the command line (cmd)?

We can easily run our program on online IDEs just by writing some code and clicking on the Run button, but what if we wanted to do it using the command-line tool or terminal or cmd ( whatever suits you😄 ). So, let’s look at how we can achieve the same.

We will tell you two ways to run a program using the command-line tool.

Way -1

  • Open your favorite editor and save our hello world program in a file. Let’s name this file hello.go
  • Now open the terminal or your command prompt and go to the directory where you saved the file.
  • Run “go fmt hello.go“command to clean up your code formatting. You can skip this step if you want to, but it will format your code correctly.
  • Now, run “go build hello.go” command, and it will compile your source code. It will create an executable file in the same directory which will be present as hello.exe in the Windows system and as hello, on the macOS or Linux system.
  • Now, we just have to run the executable file. We can do this by typing hello.exe in the Windows system and ./hello on the macOS or the Linux system.

And you will have your output of the program on the terminal. “go build” command generates an executable file that can be further distributed to multiple users. They will be able to run it even if they don’t have Go installed on their machine.

Way -2

This way is a lot easier and quicker to execute

  • Again save your program in a file and open up your command prompt or terminal in the same directory.
  • Now, just run “go run hello.go” command on the terminal, and you will have your output on the terminal.

The go run command first compiles and then executes the source files without saving the executable file in the current directory and thus making it a little faster.

The What If scenarios

What if we didn’t use the package clause?

The program will break because every Go file has to begin with a package clause.

import "fmt"

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

Output –

expected 'package', found 'import'
What if we don’t write the import statement?

The program will break if we remove the import statement for a package and still uses that package functions in the program, as shown in the below example.

package main

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

Output –

undefined: fmt

It gave “undefined: fmt” error as the program doesn’t recognize what an fmt is.

What if we write an additional unused import in the program?

In this case, the program will also break because the Go file must only import the packages used in the program, as this helps in the fast compilation of the code.

package main

import "fmt"
import "strings"

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

Output –

imported and not used: "strings"

What if we rename the main function name to something else?

The program will break here too. The Go compiler looks for the main function as the starting point for the program to run the standalone program, so it’s a must to write in a standalone application.

package main

import "fmt"

func codekru() {
	fmt.Println("Hello World!")
}

Output –

function main is undeclared in the main package

This is it for our Hello World! Program. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.