Blocks

June 12, 2023

Blocks

A block is a possibly empty sequence of declarations and statements within matching brace brackets.

Block = "{" StatementList "}" .
StatementList = { Statement ";" } .

The most common example of an explicit block is the body of a function:

func foo() { // <--- The body block start here
	/* some interesting code goes here */
} // <--- and ends here

However, you can also create an explicit block at any time, simply by placing brackes/curly braces ({ and }) around some code:

{
	fmt.Println("These two lines are")
	fmt.Println("within the same explicit block")
}

fmt.Println("This line is not")

This is not common, but it can be useful in some cases. For one thing, it might be done for visual effect. But more often, it’s done for the purpose of adding a new variable scope, which we’ll come to shortly.

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe