Your First Go Program
Start with Your First Go Program
The classic Hello World program prints a single greeting. It is small enough to understand every line, and it tests the complete path from source file to running code.
What you’ll learn
- Write a complete Hello World program.
- Explain the package, import and entry point.
- Identify the code that produces the output.
Understand the idea
Save the code below as main.go. Every executable Go program starts with package main and has a main function. The fmt package supplies convenient printing functions.
The program uses a function call: the name fmt.Println, followed by parentheses containing its argument. In this case the argument is a string literal.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Output
Hello, World!Trace what the program does
Go begins at main(), evaluates the string literal, calls fmt.Println, and writes the greeting plus a newline to standard output. The text is not stored permanently; it is displayed as the program runs.
Avoid common mistakes
- The file needs the
.goextension. - Use ordinary straight quotation marks in source code, not typographic “smart quotes”.
Try it yourself
Make the program print Hello, Go! and then add a second fmt.Println call.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Your First Go Program in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.