Every Go journey starts with a program that prints a message to the terminal. In this exercise you write the classic “Hello, World!” and get to know the structure that every Go program shares.
Statement
Write a complete Go program that prints exactly one line to the terminal: the
text Hello, World! followed by a newline.
Constraints:
- The file must be a valid
package maincontaining afunc main. - Use the
fmtpackage to print. - Print nothing else, no extra spaces or lines.
Example
This program takes no input. When you run it, it must produce this output:
Hello, World!
Starter code
package main
import "fmt"
func main() {
// Print "Hello, World!" here.
}
Hints
Hint 1
The fmt package is already imported. Look for a function that prints a value
and adds the newline for you.
Hint 2
fmt.Println writes its argument followed by a newline. In Go, text is written
between double quotes.
Hint 3
Call fmt.Println("Hello, World!") inside main. That single line is the whole
program body.
Solution
Show the solution
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}package main tells Go that this file builds an executable, and func main is
the entry point that runs when you start the program. fmt.Println prints its
argument to standard output and appends a newline, so the terminal shows
Hello, World! on its own line.
Common mistakes:
- Forgetting the
package mainline or naming the package something else, so the program cannot be run directly. - Using single quotes
'...'. In Go, single quotes are for a single rune; text values use double quotes. - Removing the
import "fmt"line, which makesfmt.Printlnundefined.
Ready for the details behind this program? Read Your first Go program.