Skip to content

How to create your first Go program

New Courses Coming Soon

Join the waiting lists

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!")

Screen Shot 2022-07-28 at 12.17.14.png

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 docsformats 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:

Screen Shot 2022-07-28 at 14.18.46.png

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.

→ Get my Go Handbook

Here is how can I help you: