Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages out there. Newer than C# but older than Rust and Swift. Google developed golang because they needed a language suited for the modern cloud infrastructure. With an easy code syntax, it also helped Google speed up the coding process.
Golang is a fast and resource friendly language. Often used for server-side and backend of applications. To create microservices, web applications and database services Go is a good choice. Despite its young age several large companies have started using Go. Some examples are Docker, PayPal, Uber and Dropbox.
How to install Go
Go is easy to install on Windows and macOS. Just download the installation file and run the installation. The installer will do the work for you. The installation instruction can be found here: https://go.dev/doc/install
For Linux, it requires a little more work. It is not hard, you just need to do a few steps to get it working. Download the Go installation file from their website, and extract it into /usr/local/go. But first, you have to make sure there is no Go installation there already. If you overwrite a Go installation it will break. After you have extracted the tar file, go ahead and run the following command.
To check if you have Go installed, and which version you have:
go version
That will output something like this:
go version go1.18.1 linux/amd64
To be able to write code you should install a text editor. I don’t know if there exist any golang ide. Visual Studio Code is a good text editor to write Go programs. Other editors work just as fine. It all depends on what you prefer. If you decide to use VS Code open the extension tab and install the official Go extension by the Google Go Team. It will help you a lot when writing Go code.
Let us look at how to write go code, or hello world in golang.
Golang Hello World Example
“Hello world” is a classic when it comes to the first programs to create. This example will show you how to create a project in Go and the bare minimum of a Go project. The first time you open a Go file in VS Code it will ask you to install some tools. Just go ahead and answer yes to install them. It will help you with the Go code. Automatic imports of library and more.
First, we need to create a folder for our little program. We can, of course, call it whatever we want. I will make a folder called HelloWorld.
Create the project
After the HelloWorld folder is created we go to the folder and initialize the project.
go mod init helloworld
This will create a mod file called go.mod. The file that contains information about the project. Like the project name. In this case the project name is helloworld, as we used in the go mod init command. Later when we import other packages it will also contains the needed information about them.
module helloworld
go 1.18
Then we can start on the program itself. Create a file called main.go and open it in your editor. In the first line of the program, we name the package. As we mentioned, everything in Go is part of a package. We call it the main package because it is the main program.
Main
package main
After we name the package, we will add the import libraries. If you are using VS Code, you don’t have to do that if the functions you are using are part of the standard libraries. VS Code will fix it for you.
The main function in Go is the entry point of the program. It is just like C and other C type languages. Main will always need to be there in any program. You can only have one main func because you can only have one entry point.
func main() {
// Code goes here
}
// is used for comments in the code and is ignored by the compiler.
To use the print function we need the fmt library. Before you use it in the program you need to import the library. If you are using VS Code try to type in the print method and put it in the main function.
fmt.Print("Hello World")
Try to save the program now. And have a look above the main function. You should see VS Code have added something like this:
import "fmt"
So the entire Hello World program source code.
package main
import "fmt"
func main() {
fmt.Print("Hello World")
}
To run this program we do
go run main.go
And you should see it print out Hello World. But it doesn’t change to a new line after the hello world message. There is two easy ways we can fix that. We can either add \n after Hello World. Or use Println in the fmt package.
fmt.Print("Hello World\n")
fmt.Println("Hello World")
Both of those will print hello world using two different print commands.
Another way to run it is to write this:
go run .
This will look for the main and run it in the current folder. The single dot means current directory.
Check out other golang tutorials and articles.