Skip to main content

Hello, World!

Introduction​

Every Go program starts with a package declaration and a main function. The main package is special — it tells the Go compiler that this file is an executable program, not a reusable library. The main() function is the entry point: execution always begins here.

Go requires all imports to be used. If you import a package and don't use it, the code will not compile. This is a deliberate design choice that keeps codebases clean — the compiler enforces what Python's linters only warn about.

Go uses curly braces {} to delimit blocks of code, not indentation. Indentation is still expected (and enforced by go fmt), but it is not syntactically meaningful. This is one of the most visible differences from Python.

GitHub Copilot tip: Select any line of your Go code, open Copilot Chat, and type /explain to get a plain-English explanation of what it does.


Concepts​

Anatomy of a Go program​

package main // Every file belongs to a package

import "fmt" // Import the fmt (format) package

func main() { // Entry point — execution starts here
fmt.Println("Hello, World!")
}

Key syntax rules​

  • Every .go file must start with a package declaration.
  • Imports must be used — unused imports are a compile error.
  • The opening { of a function must be on the same line as func, not on its own line.
  • Semicolons are not required at the end of statements (unlike Java or JavaScript).

The fmt package​

  • fmt.Println(...) — prints with a newline at the end
  • fmt.Print(...) — prints without a newline
  • fmt.Printf("Hello, %s\n", name) — formatted print, like Python's f"Hello, {name}"

Examples​

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
fmt.Print("No newline here ")
fmt.Print("— still on the same line\n")
fmt.Printf("Pi is approximately %.2f\n", 3.14159)
}

Output:

Hello, World!
No newline here — still on the same line
Pi is approximately 3.14

Comparison with Python:

print("Hello, World!")
print("No newline here ", end="")
print("— still on the same line")
print(f"Pi is approximately {3.14159:.2f}")

Challenges​

1. Ask Copilot to write Hello World​

  • Create main.go in your project folder.
  • Open Copilot Chat and ask: Write a Go Hello World program.
  • Copy the output into main.go and run it with go run main.go.
💡 Show Answer
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
go run main.go
# Output: Hello, World!

2. Use /explain on your code​

  • Select all the code in main.go.
  • Type /explain in Copilot Chat.
  • Read the explanation. Can you now explain each line to a colleague without Copilot's help?
💡 Show Answer
  • package main — declares this file as part of the main package, making it an executable.
  • import "fmt" — imports the standard library's format/print package.
  • func main() — declares the program's entry point.
  • fmt.Println(...) — calls the Println function from fmt, printing text followed by a newline.
  • { and } — delimit the function body; unlike Python, indentation alone is not enough.

3. Multiple print statements​

  • Modify main.go to print three separate lines: your name, your favourite programming language, and today's date as a string.
  • Use fmt.Println for each.
💡 Show Answer
package main

import "fmt"

func main() {
fmt.Println("Alice")
fmt.Println("Go")
fmt.Println("2026-06-16")
}

4. Using fmt.Printf​

  • Print the message "Go version 1.22 was released in 2024" using fmt.Printf with two format verbs (%s for the string and %d for the integer).
💡 Show Answer
package main

import "fmt"

func main() {
fmt.Printf("Go version %.2f was released in %d\n", 1.22, 2024)
}

The common format verbs are: %s (string), %d (integer), %f (float), %v (any value), %T (type of the value).


5. What happens without the import?​

  • Remove the import "fmt" line from your file.
  • Try to run it with go run main.go.
  • Read the error message. What does it tell you?
  • Put the import back and run again.
💡 Show Answer

You will see: undefined: fmt. Go does not automatically import packages. Every package you use must be explicitly imported. This is stricter than Python, where some built-ins (like print) are always available without importing anything.


6. What happens with an unused import?​

  • Add import "os" to your file but don't use it.
  • Try to run the code. What error do you get?

Ask Copilot: Why does Go make unused imports a compile error?

💡 Show Answer

You will see: "os" imported and not used. Go treats unused imports as compile errors to keep codebases clean — it forces developers to remove dead code. Python only warns about this through linters like flake8 or ruff; it does not prevent the code from running.


7. Formatted output with multiple values​

  • Print a sentence like: "The answer is 42 and pi is 3.14" using a single fmt.Printf call with two format verbs.
💡 Show Answer
package main

import "fmt"

func main() {
answer := 42
pi := 3.14159
fmt.Printf("The answer is %d and pi is %.2f\n", answer, pi)
}

8. Print the type of a value​

  • Use fmt.Printf with the %T verb to print the type of each of the following: 42, 3.14, "hello", true.
💡 Show Answer
package main

import "fmt"

func main() {
fmt.Printf("%T\n", 42) // int
fmt.Printf("%T\n", 3.14) // float64
fmt.Printf("%T\n", "hello") // string
fmt.Printf("%T\n", true) // bool
}

Notice that Go infers 3.14 as float64 (not float32). Go defaults to 64-bit floats, just as Python defaults to 64-bit doubles.