Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-07 14:32:54

03: Building a First Program

WARNING This exercise is in DRAFT status, so there may be errors. If you find any, please email me at help@learncodethehardway.com so I can fix them.

We now need to confirm that your setup is working by creating a first little program that does almost nothing. I'll give written instructions here, but if you get stuck just watch the video. Usually this kind of interactive installation is better viewed in a video.

Create Your Work Area

Each exercise in this course will be a separate Go project. We'll need a place to put all of these projects so do this:

cd ~
mkdir Projects
cd Projects
mkdir learn-go
cd learn-go

This will create a directory named ~/Projects/learn-go you can use. When you wan to continue with the course you type this into Terminal:

cd ~/Projects/learn-go

The ~ (tilde) character means "my HOME directory" and is different for every user on every computer. For me on Windows it's C:\Users\lcthw and on OSX it's /Users/zedshaw. By using a ~ you can consistently go home before doing anything else, so if you get lost type cd ~ and figure out what went wrong.

Create ex03

Now that you're in the ~/Projects/learn-go directory you can create the initial Go project:

mkdir ex03
cd ex03
go mod init MY/ex03

This will create a directory named ex03, change directory to it, and then create your Go project.

NOTE The command go mod init MY/ex03 creates the project, but it's a bit different from how other people use it. Typically you would put a domain name where MY to represent where the project is hosted. Since you are not hosting your code anywhere, and probably don't want to type a fake domain all the time, we'll just use MY instead. We'll change this later in the course though.

Create main.go

You are now ready to create your main.go file. The first problem you may have is getting the ~/Projects/learn-go/ex03 directory open in your text editor of choice. Remember you can do this:

start ~/Projects/learn-go/ex03

To open it with your mouse. On OSX replace start with open like this:

open ~/Projects/learn-go/ex03

Enter the Code

For this exercise we only want to get you setup and not force you to type this exactly. This is the only time I'll tell you to copy-paste. If you copy-paste after this then you're cheating yourself out of learning how to fix your mistakes. Go ahead and copy-paste this into your text editor:

View Source file ex03/main.go Only

package main

import "fmt"

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

Compile ex03

Once you think you have main.go exactly the same as mine you can compile it with this command:

go build .

If you did everything right then it'll say nothing. If you didn't get it right then you'll get errors, and that's totally normal. In fact, I probably spend more time correcting errors than not when I'm programming. All programmers do.

Running ex03

After your go build . command runs you should have a new file named ex03 in your directory. Do this to see it:

ls

This will list the files in your Terminal and you should see the ex03 there. To run it type this:

./ex03

This means "run the ex03 in this directory" with ./ meaning "this directory." If you do that then you should see this:

Hello World

Causing Errors

Let's create an example error so you can see how those work. Remove the very last character { of the main.go file and run go build . again. You should get this output:

# MY/ex03
./main.go:8:1: syntax error: unexpected EOF, expected }

Look at the main.go:8:1 line. This is the FILE:LINE:CHAR format that's common in programming language errors. It means to go to line 8 and the problem is probably on the first character. That's the one we deleted. The part after this is the error message, and if you can't figure out what that means remember to google it.

Fixing Errors

If you get errors then you have to look at the error message for the line number, go to that line number and try to see how your code is different from mine. If it's exactly the same then you have to look at every line above it.

Error messages in programming languages are not exact. They're only telling you the last place the compiler could figure out probably maybe had an error. You have to use that as a clue to find the error before that line.

Next Steps

You are now ready to code. When you start each exercise after this you'll have to do these steps:

  1. Create a directory for the exercise.
  2. go mod init MY/exercise
  3. Create the main.go file.
  4. Write your code...but not all of it.
  5. Build it with go build .
  6. Run it with ./exercise.

For example, for Exercise 06 I would type this:

mkdir ex06
cd ex06
go mod init MY/ex06
# edit main.go
go build .
./ex06

Finally, the # edit main.go is just a comment in a Terminal. It get ignored and I use them to tell you important things about that line. Don't bother typing it in.

Previous Lesson Next Lesson

Register for Learn Go the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.