Code along video
Learning objectives
In this lesson, we will write our first program, called “Hello, world!” This program, which prints the message “Hello, World!” to the console, is the classic start to any programming course. This program may seem simple, but it represents a significant first step in our journey to learn how to program in Go.
To write our first program, we will need to learn how to download Go and install a text editor that will make working with Go easy.
Code along summary
Setup
Before writing our first program, you should perform the following two installations:
- Download and install the Go programming language at https://go.dev/doc/install.
- Download and install a text editor. We suggest the integrated development environment Visual Studio Code, which can be downloaded from https://code.visualstudio.com.
Running two terminal commands
Once you have done this, open a command line terminal. In Windows, search for cmd
from your start screen. This should bring up the “Command Prompt” app as a suggestion. Click to open the app. In Mac OS, open the Terminal app, which is contained in Applications > Utilities
.
Once you have opened the terminal, type the following command (go env
), and press return.
go env
After a moment you will see quite a few lines printed to the console, which contain various parameters about the inner workings of Go. This is mostly far more advanced than anything we will do in the course, but we do want you to take a look at one line.
Scroll up in the terminal window until you see GOPATH
. On my computer (Mac OS), there’s a line that says GOPATH='/Users/phillipcompeau/go'
. On a Windows machine, it might be something like GOPATH=C:/Users/YourUserName/go
. The GOPATH
variable tells us where we should place our Go code. You can always back up your code elsewhere, but this is where your primary code should go so that Go looks by default there.
Turning off modules
We need to turn off modules, a controversial thing to say, but it will make working with Go for the first hundred hours or so a lot easier.
Run the following command in Terminal.
go env -w GO111MODULE=off
If you receive no feedback in the console, then you know that the command worked.
Opening VS Code and establishing a code directory
Open VS Code. On the left side of the page, click “Open Folder” on the left side of the page.
The window that opens on my computer defaults to my Users/phillipcompeau
home directory. Even though we know that the GOPATH
is Users/phillipcompeau/go
, the go subdirectory does not exist. So, the first thing that we should do is create this directory.
Then, click “Open”.
On the left side of your VS Code window, you will see the “go” folder shortcut appear. Next to this folder, click the folder button to create a new directory.
Name the new folder that you created src
. This is where we will place our source code for this course.
Next, create two additional subdirectories of go: bin
, which will contain executable programs that we write, and pkg
, which will contain pre-compiled source code that we can re-use.
We are now ready to start writing the ubiquitous first program of any coding course, which prints "Hello, world!"
to the console.
First, create a new subdirectory of src
, and name it hello
. Then, create a new file in the hello directory called main.go
. This file will be a plain text file, but the .go extension indicates that this file will contain code in the Go programming language.
Installing the Go extension for VSCode
We are nearly ready to write our program, but you need to perform one additional installation. Technically, this isn’t required to run Go code, but it will make working with Go code even easier.
In VS Code, click the extensions tab on the left side of the screen, which looks like a square getting added to a block of three squares. (An extension is an add-on program that provides additional features.)
Then, in the extension search bar, type go
. The top result should be an extension named Go
. Beneath it, click the button that says Install
. This extension will provide code highlighting in addition to some other features like code suggestions.
Writing our hello world program
Close any messages that VS Code has provided, and click the file icon on the left tab to bring up the file explorer.
You should see the main.go
file that you created in your center window. We will type our program in this file.
First, type the following line at the top of the file. This indicates to Go that the code contained in this file will be executable (i.e., we will run this code as a program instead of having it as helper code for another program.)
package main
We are going to be printing a message to the console, a task that will require us to import some code from a repository of prewritten code called a package. The package that we need to import is called
. After adding the package import, "fmt"
main.go
should look as follows.
package main import ( "fmt" )
The part of our code that we wish to run must go into a block of code called func main()
. The block is also contained within brackets, as follows.
package main import ( "fmt" ) func main() { }
Now we just need to add our print statement. To print a message on a single line, we use the function Println()
from the fmt
package. (We will say more about functions in the next code along.) In particular, we use the command fmt.Println()
and place the message to print inside quotation marks within the parentheses.
package main import ( "fmt" ) func main() { fmt.Println("Hello, world!") }
Before continuing, the most important thing that we need to do is save our code. A solid white circle at the top of main.go
indicates that your code is not saved.
Save your code, and it will remove the solid white circle.
Navigating using the command line
We will run our program in the terminal, so open a terminal window. When you look at the terminal, you will likely see your home directory appear at the left side of the window. For example, the current line in my terminal window shows phillipcompeau@Mac-mini-2
. This indicates to me that the terminal considers its present working directory to be my home directory, Users/phillipcompeau
.
We already know that our Go code is contained within a go/src
directory that lives inside of this home directory. So, how can we navigate there in the terminal application?
First, let us type the command cd go
, and then press the return key to execute the command. The cd command tells the terminal to “change directory” into the folder indicated after the command, which in this case is the go
directory.
cd go
You will see that we are in the Go directory, because the start of the current line will show the home directory followed by Go. On my computer, it shows phillipcompeau@Mac-mini-2 go
.
The cd
command allows us to navigate to a folder within the current directory, but it also allows us to return to the parent folder using the command cd ..
; execute this command below, and you will see that we have returned to the home directory.
cd ..
We now want to navigate into the directory that contains our program, which is go/src/hello
. We could do this by executing three separate commands: cd go
, then cd src
, and then cd hello
. However, we will instead execute a single cd
command to take us directly to go/src/hello
.
cd go/src/hello
You should now see the word hello appear in your command line to indicate that the navigation was successful. On my computer, it shows phillipcompeau@Mac-mini-2 hello
.
Before we run our program, we will point out two additional helpful terminal commands. The command pwd
will tell you the present working directory, which in my case currently displays /Users/phillipcompeau/go/src/hello
. The command ls
will give the contents of the present working directory; execute this command, and you will see our main.go
file.
Compiling and running our program
Shortcut: running code within VS Code
Profiling some common compiler errors
Congratulations!
Congratulations on building your first program! This is one of the hardest things to do in programming, and it’s all downhill from here. You are now ready to start learning the basics of Go. Happy coding!