Skip to main content

Statements

A statement is a complete instruction that tells the computer to do something.

Think of it like a sentence in English:

  • A sentence = a statement in Go.
  • Each statement ends when the line ends (Go automatically inserts semicolons for you, so you don’t type ; most of the time, does not show up in the source code).

Automatic Semicolon Insertion

How it works

  • In most languages, you end a statement with a semicolon ;.
  • In Go, you can write semicolons, but usually you leave them out — the Go compiler automatically adds them at the end of a line when it thinks a statement is finished.

The rule is:

  • If a line ends with something that can legally end a statement, Go inserts a semicolon there.

Why the following example breaks

You wrote:

func main()
{

Here’s what the compiler sees:

  1. It reads func main()

  2. That could be a complete statement (in some contexts), so Go inserts a semicolon:

func main();
{ // ← now this looks like a syntax error
  1. Now { is floating alone, not attached to the function definition — error.

Correct Way

func main() {
fmt.Println("Hello World!")
}

By putting { on the same line, Go doesn’t insert the semicolon after func main().

Why Go enforces this

  • It keeps code style consistent (Go is opinionated about formatting).
  • It works hand-in-hand with the go fmt tool that auto-formats Go code.