Skip to main content

Write Your First Go Program

Let’s write a simple Go program and learn the basics of Go’s structure. By the end of this guide, you’ll understand how to create, run, and build Go programs.

Your First Program

1

Create a project directory

First, create a directory for your Go program:
# Create a new directory
mkdir hello

# Navigate into it
cd hello
2

Create the source file

Create a file named main.go:
# This creates and opens the file in VS Code
code main.go
Or create it with your favorite text editor.
3

Write the code

Add the following code to main.go:
package main

import "fmt"

func main() {
    fmt.Println("Hello Gopher!")
}
Save the file.
4

Run the program

Return to your terminal and run:
go run main.go
You should see:
Hello Gopher!
Congratulations! You’ve just run your first Go program!

Understanding the Code

Let’s break down what each part of the program does:
// package main is a special package
// it allows Go to create an executable file
package main
Every Go file starts with a package declaration. The main package is special - it tells Go this is an executable program, not a library.
// import keyword makes another package available for this file
// import "fmt" lets you access the fmt package's functionality
import "fmt"
The import statement brings in other packages. Here we’re importing fmt (format), which provides I/O functions like Println.
// func main is the entry point
// Go will automatically run this function when the program starts
func main() {
    fmt.Println("Hello Gopher!")
}
func main() is the starting point of every Go program. When you run your program, Go automatically calls this function first. fmt.Println() is a function from the fmt package that prints text to the console followed by a newline.
Functions from other packages are accessed using dot notation: packageName.FunctionName(). Notice that Println starts with a capital letter - that’s because only capitalized names are exported (visible outside the package).

Running vs Building

Go provides two main ways to execute your code:

go run - Run Directly

go run main.go
This compiles and runs your program in one step. Great for development and testing!
If you have multiple .go files, you can run them all with: go run .

go build - Create an Executable

go build main.go
This compiles your code into an executable file:
  • On macOS/Linux: ./main
  • On Windows: main.exe
You can then run the executable directly:
# On macOS/Linux
./main

# On Windows
.\main.exe
Use go run during development for quick iterations. Use go build when you want to create a distributable executable.

Try It Yourself

Now let’s practice with an exercise:
package main

import "fmt"

func main() {
    // TODO: Print your name using fmt.Println
    // TODO: Print your best friend's name using fmt.Println
}

Exercise Instructions

  1. Create a new file or modify your existing main.go
  2. Print your name using fmt.Println()
  3. Print your best friend’s name using another fmt.Println()
  4. First, run it with go run main.go
  5. Then, build it with go build main.go and run the executable

Going Further

Let’s enhance our program with comments and Unicode support:
package main

import "fmt"

// func main creates the entry point for Go programs
// After compiling, Go runtime will first run this function
func main() {
    // Println is an exported function from the "fmt" package
    // Exported means the first letter is uppercase
    fmt.Println("Hello Gopher!")
    
    // Go supports Unicode characters everywhere!
    fmt.Println("Merhaba Köstebek!") // Turkish for "Hello Gopher!"
    fmt.Println("你好 Gopher!")        // Chinese
    fmt.Println("Привет Gopher!")     // Russian
}
Go has excellent Unicode support. You can use any Unicode characters in strings and even in your source code comments!

Common Commands Reference

Here are essential commands you’ll use frequently:
CommandDescription
go run main.goCompile and run your program
go run .Run all .go files in current directory
go build main.goCompile into an executable
go buildBuild the current package
go fmtFormat your code (do this often!)
go doc fmt PrintlnView documentation for a function

Next Steps

Now that you can write and run Go programs, you’re ready to dive deeper into Go fundamentals:

Packages & Imports

Learn how to organize code with packages

Variables & Types

Understand Go’s type system and variables

Functions

Master functions and control flow

Exercises

Practice with hands-on exercises
Remember: the best way to learn programming is by doing. Try modifying the examples, experiment with the code, and don’t be afraid to make mistakes!

Build docs developers (and LLMs) love