Skip to main content

Command Palette

Search for a command to run...

How to Print "Hello World" Using Go

Updated
2 min read

Go is a language that is increasing in popularity, and for good reason. With Python's easy-to-understand syntax and C's efficiency, more and more companies are starting to use Go as a programming language in their software. As a software developer, Go is a great programming language to know, and the best way to start learning a language is to start off with the absolute basics. This tutorial will teach you how to get started with Go by writing a script to print "Hello World."

Starting off with a blank file, we first have to import a library called 'fmt' which stands for 'format'. This module has formatted I/O functions that allow for the printing/scanning of user input.

import "fmt"

The code is going to reside in a function called 'main', and we can define this function like this:

func main() {}

Inside this function, we can use the 'Println' function found in the 'fmt' library to print 'Hello World'.

fmt.Println("Hello World")

And so the final script is this:

import "fmt"

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

Congratulations, you have written your first script in GO! After writing the script, we would like to execute it, and this can be done through the following steps.

Let's say the 'Hello World' script exists inside a file called 'main.go' as shown below. Note that at line 1, we specify that this file belongs to the package 'main'. This is because every file written in Go must belong to some sort of package, otherwise Go will give an error.

In the terminal, we can then write the command 'go run main.go' to execute the script. After writing this command, you will see 'Hello World' outputted in the terminal. You have now successfully written and executed a Go script!

More from this blog

GoTech With Alissa

19 posts