Getting Started with Go

Hello World

The first thing everyone does when learning any new language is to create a Hello World program.

Create a project folder called getting-started and create a file in there called main.go. Open the folder in VS Code and copy the code below into the Go file.

package main

import "fmt"

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

Every Go program must start with a “main” package declaration. The main package tells the Go compiler that the file should be compiled as an executable and not a shared library. The main() function inside the “main” package becomes the entry point into our program.

Below the package declaration, we have our import statements. We could create our own packages or use third-party packages. This is the place to import them. The fmt package is mostly used to print messages to the terminal window.

The main function is where our program starts and ends. In our Hello World program, we only have a single statement that prints “Hello, World!” to the terminal.

To run this code in VS Code, click the Terminal menu > New Terminal and enter the command:

go run main.go

Here, go is a tool for managing Go source code. It was installed on your system as part of the Go programming language. The run command is used to compile and run Go programs. And main.go is the file to compile and run.

You should see the following output.

Hello, World!

Packages

Packages are a way Go organizes code, makes it reusable, and lets us share it with others who wish to use it in their own programs. Each package will have its own set of pre-written code and functions that we can use.

Visit the Go documentation page for fmt and look at all the functions available inside that package. There are functions that print data, formats data, and reads user input from the terminal.

In the Hello World program we used the fmt package and one of its functions called Println() to print a message to the console window. Printing information to the console is an essential task of programming in any language. We’ll be doing much more of this throughout our Go lessons.

There are many other packages in Go’s standard library that do things like creating a web server, html templating, error handling, and so on. You can also find excellent Go packages online and GitHub such as Fiber for web apps, GORM for object-relation mapping, and Goldmark for parsing Markdown.

When you use external packages, those packages become dependencies. These dependencies should be managed as the packages themselves receive feature or security updates over time. Go provides dependency management tools that help orchestrate this task. We look more at this in latter lessons but if you’re eager to learn now, check out Go’s documentation for managing dependencies.

Declaring Variables

Declaring variables in Go is the same as in other languages, but different. There are two ways to declare variables in Go, the var keyword and the short declaration operator.

When declaring variables outside of functions, you must use the var keyword. The following are all valid was of declaring variables.

var i int
var l, m, n, o float64
var j = 10
var x, y int = 1, 2

If you declare a variable using the var keyword without a value, such as var i int, zero values get assigned. If the variable is an integer 0 gets assigned, empty strings for string variables, 0.0 for floats, and so on.

The second method of declaring variables is to use the short declaration operator :=. This shorthand for variable creation lets you write less code but it only works when creating local variables inside of functions.

A single equal sign = can be used to assign new values to existing variables.

Update the main() function in the Hello World program with the following code to see how that works.

func main() {
	var i = 10
	x := 31
	x = 16
	fmt.Println(i, x)
}

A common error you encounter when programming in Go is that variables must be used when they are defined within functions. The Go compiler does not let you declare a variable inside a function and not use it.

A general best practice is to limit the scope of the variable as much as possible. This means declaring and using the variable where it’s needed.

Summary

In this post we created a Hello World app in Go, introduced packages, and looked at how variables are created. In the next lesson, we look at Go’s data types.