Start learning free

LESSON 32 OF 338 · FUNCTIONS I

Function Bodies Need Braces in Go: Quiz

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

func showWeather()
    fmt.Println("It's sunny!")

showWeather()

Important Note:

Do not use an actual code editor to get the answer. It would defeat the purpose of the quiz.

Pick your answer

Next lesson

GO · FUNCTIONS I

Quiz Indentation 1 in Go, explained

The question

The quiz shows code that looks like it could belong to a language where indentation marks the function body:

func showWeather()
    fmt.Println("It's sunny!")
showWeather()

It asks what happens if you run it. A: it prints It's sunny!. B: it prints nothing. C: it prints It's sunny! twice. D: it produces a syntax error because a Go function body must be wrapped in curly braces.

Reasoning about the code

Start with what a human would see: a header, an indented body and a call at the bottom. That would be a sensible program if indentation defined the body. In Go it does not.

  1. Look for the braces. A Go function body is the block between { and }. This code has neither.
  2. Read the header the way the compiler does. func showWeather() followed by a line break is a complete declaration without a body. It is not a function you can call.
  3. Look at what follows. The indented fmt.Println line is then a statement at the top level of the file. The last line, showWeather(), is a statement there too. Statements aren't allowed outside function bodies, so the compiler reports a syntax error such as non-declaration statement outside function body.
  4. Remember what a compile error means. The program is never built, so it can't print anything, not even a partial result.

That points to D. Options A and C assume the code runs. Option B assumes it runs and prints nothing, but the code never runs at all.

What the code would need

With braces, the function is valid and the indentation becomes just good style:

func showWeather() {
    fmt.Println("It's sunny!")
}

A call to this function prints It's sunny! once. The opening brace must sit on the same line as the header. If it moves to its own line, Go reports unexpected semicolon or newline before {, because Go inserts a semicolon at the end of a line that ends with a closing parenthesis.

A valid program that calls it has this shape, and it prints It's sunny! once:

package main

import "fmt"

func showWeather() {
    fmt.Println("It's sunny!")
}

func main() {
    showWeather()
}

Notice that the call moved inside a function, main. In the quiz, the bare showWeather() at the bottom of the file would be a second problem even if the braces were present, because Go does not allow a call at the top level.

The solution in Go

var answer string = "D"
/* Explanation:
Go doesn't use indentation to decide what belongs to a function: a function's body must be wrapped in curly braces.
Here func showWeather() is followed by a new line instead of {, so the compiler stops with a syntax error and nothing is printed.
*/

The line declares answer as a string holding "D". The quotes make it text, and the capital letter must match the option exactly. The block comment between /* and */ is ignored by the compiler.

Common mistakes

  • Treating indentation as structure. Go formatters such as gofmt indent your code for readers, but the compiler only follows braces.
  • Picking A because the intent is clear. The compiler doesn't read intent. It follows the grammar.
  • Picking C. Nothing in the code could print twice: it can't even be compiled once.
  • Choosing B because a function with no call prints nothing. That reasoning is right for valid code, but this code is invalid.

Key takeaways

  • A Go function body always sits in curly braces, with the opening brace on the header line.
  • Indentation is a convention for readers and has no effect on meaning.
  • A syntax error stops the whole program before any line runs.
Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free