Run your first Go program
Meet package main, the main function, and fmt.Println.
- Create a Go program that prints a line of text.
A Go source file declares a package. An executable starts in func main() inside package main. Import fmt to format and print values. Go keeps programs intentionally simple: names are case-sensitive, and every imported package must be used.
1package main
2
3import "fmt"
4
5func main() {
6 fmt.Println("Hello, Go!")
7}Hello, Go!
fmt.Println prints its arguments and ends with a newline. The package main declaration marks this as an executable program. The compiler checks your imports and syntax before it runs.
Key takeaways
Create a Go program that prints a line of text.
Go catches many mistakes while compiling.
Use small experiments to understand each new language feature.
Lesson quiz
4 questions · pass with 3 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Go
Write Go in the editor and run it against sample input with a remote compiler. Your code is sent to Compiler Explorer to compile and run.
Print a greeting
Complete main so the program prints exactly Hello, Go! on one line.
- Greeting
Go is compiled and run by Compiler Explorer (godbolt.org), a free public service. Your source code and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…