Getting Going with Go, Day 2
Tags: codenewbie, programming, go
Published: 2021-01-26 17:21:59

[Yesterday, I outlined](https://dev.to/kaipmdh/getting-going-with-go-day-1-bkl) 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:

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

```

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:

```bash
kai:~/helloworld/ $ go build 
```
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!

```bash
kai:~/helloworld/ $ ./helloworld
Hello, World!
```

### 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

- [How To: C/C++ Main](https://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php)

- [James Mills' "Learn Go in Five Minutes" GitHub gist](https://gist.github.com/prologic/5f6afe9c1b98016ca278f4d507e65510)

- [Package fmt documentation](https://golang.org/pkg/fmt/)

- ["Understanding Golang Packages" on The New Stack](https://thenewstack.io/understanding-golang-packages/)

- Cover image: Photo by [Dominik Scythe on Unsplash](https://unsplash.com/@drscythe?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

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