Go tutorial: Hello world

This tutorial helps you get started with Go. It explains what you need to write your first Go program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and running your code. If you have never written a Go program before this is the place to start. We have created this article, so you can get started with Go, and move on to our sms examples. If you are already familiar with Go, you can jump directly to one of the following SMS projects.

Go sms examples:

Go send sms with the HTTP rest api (code sample)
Go send multiple sms with the HTTP rest api (code sample)
Go schedule sms with the HTTP rest api (code sample)
Go receive sms with the HTTP rest api (code sample)
Go delete sms with the HTTP rest api (code sample)
Download the latest Go sms api library from Github

What is Go

Go is a programming language, that was developed by Google. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that programming languages have a more rigorous structure, to help the computer understand it better.

What is a Go 'hello world' program

The Go hello world program is the simplest program you can write. It simply prints out the sentence: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Go

To write your first program in Go:

  1. Check prerequisites
  2. Setup Go
  3. Test if Go is installed
  4. Open Notepad
  5. Create a folder for Go files
  6. Write Hello World program in Go
  7. Run your Go Hello World program
  8. Check the output

Prerequisites

Here is what you need to get started. To write your first computer program in Go, you need a Windows computer, the go language and example code presented below.

  • Windows 10 computer
  • Go language
  • Ozeki Hello World example project

Download the Go language

You might want to use this link: https://golang.org/ to download Go.

Video 1 - How to download Go (Video tutorial)

Download Go

To get started with coding in Go, you must first download it from the official website. Visit the link https://golang.org/. Click on the blue Download Go button (Figure 1). This will start downloading the installer to your browser's default download location.

how to download go
Figure 1 - Download Go

Install the Go language

Video 2 - How to install Go (Video tutorial)

To test if Go is installed, we have to open the Command Prompt and type in the following command:

//This command will return the current version of the Go on our computer
go version
	

Code 1 - Testing if Go is installed

Test Go

You can test if Go is successfully installed in the Command Prompt. Open the command prompt first. Type in "go version" and press enter (Figure 2). If the command prompt returns the current version of go installed, it is in working order.

how to test if go is installed
Figure 2 - The version of Go installed on our computer

Write your 'hello world' program in Go

To write our first Go program we will use Windows Notepad.
To create our first Go project we have to create a folder where we will place our files for the development.
We can do this manually or if you are familiar with the command prompt you can create your files using that.
In the following video I'm going to show you how to create your first Go project.

//If you want to use a different name for the project you can replace the helloWorld part
go mod init helloWorld
	

Code 2 - How to initialize a Go project

package main

import "fmt"

func main() {
	fmt.Println("Hello world!");
}
	

Code 3 - Your 'hello world' program in Go

Video 3 - How to write a 'hello world' program in Go (Video tutorial)

Run your 'hello world' program in Go

To run your code in Go, we have to use the command prompt.
To run our first Go code we will use the following command:

//This command will run the go code which has the main package in it
go run .
	

Code 4 - Run your first Go program

Output of your code

To run your program, simply launch a command prompt in your program's directory. Type in "go run ." and press enter (Figure 3). This will execute your code. The "Hello World!" message will appear in the console. This is the output of the program.

output of a go program
Figure 3 - The output of the code

Video 4 - How to run your first Go program (Video tutorial)

What if you make an error in your Go program

If you make a mistake, when you write your instructions in Go, you will get a Syntax error. The computer will highlight the line with error in red, and it will tell you why it didn't understand the instructions. To correct the mistake, you need to go back to the text editor and modify the program. Programs say they "fix the error" when they correct mistakes.

In the code below we will create a mistake intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Go and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Go?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Video 5 - What happens if you make an error in your Go code (Video tutorial)

Errors in Go

When there is an invalid command in your Go code it will lead to an error. To demonstrate this, we are going to delete one of the quotation marks after "Hello World". This will create a syntax error when you run the program (Figure 4). This error message contains the details of why the error occurred and where.

error feedback in go
Figure 4 - Error report in Go

Summary

The article above is about the things you need to provide before starting to code in Go and create your first Hello World program. This program may seem simple, and it may not do much, but starting with small steps results in accomplishing great things. By following this guide, you have made your first step in becoming a Go developer; creating programs as a solution for your problems will help you a lot to make the work smoother.

What you have learned here is valuable, there are further useful guides on the Ozeki website to read. Check out the next article on this topic about how to send SMS from Go.

Start the change, download the Ozeki SMS Gateway now!

More information