DEV Community

Cover image for Getting Going with Go, Day 2
Kai
Kai

Posted on

Getting Going with Go, Day 2

Yesterday, I outlined what I was looking to do and achieve with this learning diary, and got set up with Go by installing it and an extension for VSCode to help us along.

Today, I'm going to write my first Go program. Baby steps!

Hello, World!

Writing the program

Today's task is to be writing the classic starter program for any language, one that outputs a "hello world" message to the screen. I'll try to go through the code line by line and come to an understanding of what it does. Of course, this is only scratching the surface, but shows the structure of the language.

Let's start in a directory called helloworld, where we will create a file called main.go. Into main.go we'll put the following, which I've marked with line numbers. You'll need to leave them out when you type your program in:

1 package main
2 
3 import "fmt"
4 
5 func main() {
6   fmt.Println("Hello, World!")
7 }

Enter fullscreen mode Exit fullscreen mode

Before we get antsy and run the program, let's run through what we've written line by line.

Line 1, package main is our definition where we say that this package we are writing is to be the main executable of our program. If we were to create other packages that we use in our program, we would have to declare them as such in their own files.

Line 3, import "fmt" says that within this program of ours, we'd like to use the fmt package. fmt is part of the Go standard library. It provides for formatting functionality of input and output, which means that we can use it to print to the screen. Fairly fundamental stuff. If your program wasn't ever going to print any output or take in any input, you wouldn't need to import the fmt package, saving on your program's footprint.

Line 5 is the start of our main function. main() is a special type of function , which is the one that gets executed first when the program is called. Think of it as the doorway into the program. Having this structure makes sense, as Go aims to build and improve on standards and structure set by the C family of languages.

Functions in Go are defined with the syntax func functionname() and a pair of braces / curly brackets ({}). In the case of our program above, the function main, including its declaration and delimiter braces, encompasses lines 5-7.

Line 6 is the command to print our message to the screen. It uses the Println (print line) method of the fmt package, with the contents to be printed enclosed in parentheses. Because we are printing text, it needs to be enclosed in quotation marks. You can of course play around with the contents of the string "Hello, World!" to have it print whatever you want. Just make sure that you enclose everything you need to be shown on the screen by the Println method in quotation marks. It won't work if you don't.

Line 7 rounds out our main function by closing it off with the closing brace. If you wouldn't, the computer wouldn't know where the thing is meant to end, and running the program would fail.

Running our program

Unlike interpreted languages like Python or Ruby, we can't "just run" our program. Instead, we'll have to build it to an executable:

kai:~/helloworld/ $ go build 
Enter fullscreen mode Exit fullscreen mode

This packages everything we need to run the program into machine-readable code. If there were no errors, you should see no output, but when you list the files in the directory, you should see an executable file called helloworld, the same as the directory it's in.

If you run the executable, you should see your program output a friendly greeting!

kai:~/helloworld/ $ ./helloworld
Hello, World!
Enter fullscreen mode Exit fullscreen mode

Editing our program

If you wanted to change the output of the text from "Hello, World" to something else, you would need to save the file and run go build again. Changes to your compiled program won't appear if you just make them in the source code.

References and Further Reading

Next, I think we'll look a little bit deeper into compiling our program and Go modules.

Top comments (1)

Collapse
 
marcuskohlberg profile image
Marcus Kohlberg

Hey Kai,
Great to see you creating content like this. I think having a transparent diary like this will inspire more folk to "get going with Go".
Want to let you know that you can use encore.dev for free if you want to built something and make it available in prod without having to set anything up.

Full disclosure: I'm part of the Encore team, but just trying to be helpful here. :)