Your First Program
What you will learn
You will turn an empty folder into a working Go program, and you will understand every file and command involved. You will also break the program on purpose, because reading compiler errors is a skill you will use every day.
Create a project
Every Go project is a module. Start by making a folder and initialising one:
mkdir hello-go
cd hello-go
go mod init example.com/hello-go
The last command creates one file, go.mod. Open it:
module example.com/hello-go
go 1.26
The module line is the name of your project. It also becomes the prefix for import paths if you split your code into packages later. The go line records which Go version the project targets, and yours will match the version you installed. As you add third-party libraries, go.mod will also list them.
example.com/hello-go is a placeholder, which is fine for practice. For a project you plan to publish, use its real repository path, such as github.com/yourname/hello-go.
Write main.go
Create a file named main.go in the same folder:
package main
import "fmt"
func main() {
name := "Gopher"
fmt.Println("Hello,", name)
}
Now read it one line at a time:
| Line | Meaning |
|---|---|
package main | Every Go file belongs to a package. The package named main is special: it tells Go to build an executable program rather than a library. |
import "fmt" | Brings in the standard library package fmt (short for "format"), which handles printing text. |
func main() { | Declares a function named main. When a program starts, Go runs this function first. |
name := "Gopher" | Creates a variable called name holding the text "Gopher". Go works out that it is a string. |
fmt.Println("Hello,", name) | Calls the Println function from fmt. It prints its arguments separated by spaces, then a newline. |
} | Ends the function. |
Notice that Println starts with a capital letter. In Go, a capital first letter means a name is visible outside its package. That is why you can call fmt.Println but could not call a hypothetical fmt.println. You will see this rule again in the packages lesson.
Run it
go run .
You should see:
Hello, Gopher
The dot means "the package in the current folder". Behind the scenes, go run compiles your code into a temporary executable, runs it, and deletes it. It is the command you will use most while learning.
Format it
Go has one official code style, applied by a tool. Make your code messy on purpose:
package main
import "fmt"
func main(){
name:="Gopher"
fmt.Println("Hello,",name)
}
Then run:
go fmt .
The file is rewritten with consistent tabs, spacing, and line breaks. This is why Go projects look alike. There are no style arguments, and you never format by hand. If you ever need to format a single file, gofmt -w main.go does the same job. With the editor setup from the previous lesson, this happens every time you save.
Build an executable
go run throws its executable away. When you want to keep one, use:
go build .
This creates a file named after your module folder: hello-go on macOS and Linux, hello-go.exe on Windows. Run it directly:
./hello-go
On Windows use .\hello-go.exe. You can copy that single file to another machine with the same operating system and processor, and it runs without Go installed.
Break it on purpose
Compiler errors are Go's way of helping you. Try each of these changes, run go run ., and read the message. Then undo the change.
A typo in a function name. Change Println to PrintLn:
./main.go:7:6: undefined: fmt.PrintLn
The message gives the file, line, and column, then the problem. Go is case-sensitive, so PrintLn and Println are different names.
A missing import. Delete the line import "fmt":
./main.go:6:2: undefined: fmt
You used a package you never imported.
An unused import. Add import "os" below the fmt import without using it:
./main.go:5:2: "os" imported and not used
An unused variable. Add count := 3 inside main and never use it:
./main.go:8:2: declared and not used: count
These last two surprise newcomers. Go treats unused imports and variables as errors, not warnings. It feels strict, but it keeps code free of leftovers.
Common mistakes
go: go.mod file not found. You ran a Go command outside a module. Change into the project folder or rungo mod init.- Running
go run main.goand getting confused by multiple files. Usego run .so Go builds the whole package, not one file. - A package name other than
main. If the file sayspackage hello, Go builds a library andgo runfails, because there is nothing to execute. - Forgetting that
mainmust be lowercase. Go looks for exactlyfunc main().
Practice
Create variables for your name, the language you are learning, and the number of days you have studied. Print one readable sentence using all three. Then print the same sentence again using fmt.Printf with %s for strings and %d for the integer.
package main
import "fmt"
func main() {
name := "Asha"
language := "Go"
days := 3
fmt.Println(name, "has studied", language, "for", days, "days")
fmt.Printf("%s has studied %s for %d days\n", name, language, days)
}
Output:
Asha has studied Go for 3 days
Asha has studied Go for 3 days
Println joins its arguments with spaces and adds a newline. Printf takes a template where %s and %d are placeholders filled in from the arguments that follow, in order. Unlike Println, Printf does not add a newline, so the template ends with \n. Try changing %d to %s and run go vet . to see Go warn you about the mismatch.
Summary
A Go project is a module, created with go mod init, which produces a go.mod file. An executable starts in package main at func main(). Use go run . to try your code, go fmt . to format it, and go build . to produce a file you can keep. Compiler errors point to an exact file, line, and problem, and reading them carefully is part of the normal edit cycle.
Official references: How to write Go code and Tutorial: Get started with Go.
Ready to wrap up?
Mark this complete when you can explain the main idea and finish the practice task.