ProgramGuru

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.

Visual explanation for hello worldA main function executes a printing call and then returns.Start main()Step 1Evaluate stringStep 2Call PrintlnStep 3Print greetingStep 4A main function executes a printing call and then returns.
A main function executes a printing call and then returns.

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 .go extension.
  • 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

Which part of the example actually prints the greeting?
Choose one answer. You can try again.

Answer and explanation

fmt.Println("Hello, World!")

Correct. Go begins at main(), evaluates the string literal, calls fmt.

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.

    Next lesson: Structure of a Go Program →