How to create your first Go program
After the introduction to the Go programming language we’re ready to create our first Go program!
It’s a programmers tradition to make the first program print the “Hello, World!” string to the terminal when it’s ran. So we’ll do that first, and then we’ll explain how we did it.
Maybe you have a folder in your home directory where you keep all your coding projects and tests.
In there, create a new folder, for example call it hello.
In there, create a hello.go file (it can be named as you want).
Add this content:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

This is your first Go program!
Let’s analyze this line by line.
package main
We organize Go programs in packages.
Each .go file first declares which package it is part of.
A package can be composed by multiple files, or just one file.
A program can contain multiple packages.
The main package is the entry point of the program and identifies an executable program.
import "fmt"
We use the import keyword to import a package.
fmt is a built-in package provided by Go that provides input/output utility functions.
We have a large standard library ready to use that we can use for anything from network connectivity to math, crypto, image processing, filesystem access, and more.
You can read all the features that this fmt package provides on the official documentation.
func main() {
}
Here we declare the main() function.
What’s a function? We’ll see more about them later, but in the meantime let’s say a function is a block of code that’s assigned a name, and contains some instructions.
The main function is special because what’s where the program starts.
In this simple case we just have one function, the program starts with that and then ends.
fmt.Println("Hello, World!")
This is the content of the function we defined.
We call the Println() function defined in the fmt package we previously imported, passing a string as a parameter.
This function according to the docs “formats according to a format specifier and writes to standard output”
Take a look at the docs because they are great. They even have examples you can run:

We use the “dot” syntax fmt.Println() to specify that the function is provided by that package.
After the code executes the main function, it has nothing else to do and the execution ends.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.