Hello world is a simple but classic computer program that introduces new users to the syntax of a specific programming language. A hello world program contains the most basic and yet fundamental parts of a program in the specified programming language.
When the hello world program is executed, it displays the string “Hello, world!” on the screen. This tutorial will explore the hello world program in Go programming and break it down to its bare essentials. Doing so will, in return, help you understand the syntax of the go programming language and how to use its various features.
NOTE: Before proceeding to write your first hello world program in go, ensure you have the Go compiler installed and configured for your system.
Learn how to install Go in Windows, Linux, and MacOS in the resource below:
https://linuxhint/com/how-to-install-go
Writing Hello World in Go
Let us start by creating our first hello world program in go.
Start by creating a directory to store your hello world program.
Navigate into the directory using the cd command:
The next step is to enable dependency tracking for your code. Dependency tracking allows go to manage packages imported in other modules. Although this is not critical for a simple hello world program, it is a good practice.
You enable dependency tracking in go by creating a file in go.mod file. This file will track the modules where you import the packages in your code.
In the terminal, run the command below to create an example module as:
go: creating new go.mod: module example/hello-world
Create a file to store your hello world program. A go program source code is stored in the file ending with a .go extension.
Edit the file using your favorite text editor:
Add the code below and save the file.
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Go Hello World Breakdown
Let us breakdown the individual components of the hello world program above.
Package Main
The first line of the go program is the “package main”.
This clause acts as a starting point of the program. A special block tells the go compiler it should compile the source code as a single executable and not a shared library.
In go, there are two types of programs: a standalone executable and a shared library. We can execute a standalone executable straight from the terminal, and the program will perform its desired operations.
A library contains code that other parts of the program can share and reuse.
Import “fmt”
Adjacent to the “package main” clause is the “import “fmt”” statement. This clause is used to import fmt package.
In go, we import packages using the keyword import, followed by the package’s name to import.
In our case, we need the fmt package, which is used for Input/Output operations. This allows us to print something on the screen.
Func main
Next is the “func main” block. This is used to define the main function of your program. This is where the program will start executing.
To create a function in go, we start the keyword “func” followed by the name of the function and two parentheses. You can pass parameters to functions by passing them inside the parentheses.
NOTE: The main function in go does not have any parameters or a return value.
Next, add a pair of curly braces. This denotes where the code for the function lives.
The main function will typically call upon other functions to perform other tasks.
Fmt.println()
The fmt.Println() is a function that is called upon the main function. It has two major parts:
- Fmt–the name of the package where the method is located.
- Println()–the actual name of the method.
We use the Println() method, or print line, from the fmt package to print the string “Hello, world!”.
Run a Go Program
To run a go program, you need to compile it first. Ensure you have the go compiler installed on your system before proceeding.
To run the program, use the command:
The “hello-world.go” refers to the name of the file containing the go source code. Once we run the command above, we should see the output of the program as:
Hello, world!
You can also create an executable using the go build command:
The command will build the code into a standalone executable, which you can run from the terminal as:
Or if you are on Windows:
Closing
This guide provides a comprehensive breakdown of the structure and concepts of the hello world program in go programming language. If you are new to go, “Welcome to the club” and keep practicing to enhance your skills.
Thank you for reading, and stay tuned for more Go tutorials.