Scope within functions

Today we’re rounding out the list of scoping rules, with two more: Declarations and scope … The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block. The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block.


Scope of type parameters

If you missed it, we had a great Boldly Go: Live livestream yesterday. I built a basic linter from scratch. It was easier than I expected, and we all learned a few things. It’s not too late to catch the replay if you’re interested! Type parameters are what power generics, and they have two specific scoping rules: Declarations and scope … The scope of an identifier denoting a type parameter of a function or declared by a method receiver begins after the name of the function and ends at the end of the function body.


Scope of the function block

Join me later today for my second Boldly Go Live stream! Declarations and scope … The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body. This is pretty intuitive. When declaring a function or method: func sum(a, b int) (result int) { /* ... */ } func (a Animal) Speak() { /* ... */ } The scope of any variables mentioned in the function signature (method receiver, function parameter, or result variables), are the function body.

Go Language Spec

9 min watch


Constants can't overflow, and what that means for you

Constants in Go don't overflow, by definition. Why? And what does this mean?

Subscribe to Boldly Go: Daily

Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.

Unsure? Browse the archive.


Scope of imported package names

Continuing our examination of scope… Declarations and scope … The scope of the package name of an imported package is the file block of the file containing the import declaration. The only thing that goes in the file block are imported package names. This is why you must repeat an import statement for every file that uses a symbol from the imported package. So for example, given two files in the same package:


The universe and package blocks

Let’s now dive into the specific scoping rules in Go. There are 8 rules here, and we’ll take one or two per day… Declarations and scope … Go is lexically scoped using blocks: The scope of a predeclared identifier is the universe block. The “universe block”, which encompasses all Go source text, only contains the predeclared identifiers defined in the specification. Specifically, these identifiers are the names of predeclared types, the constants true, false, and iota, the zero value nil, and the built-in functions.


Scope, defined

Declarations and scope … The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, label, or package. This definition isn’t necessarily specific to Go. Most modern languages have a concept of scope that’s pretty similar. What mainly differs between languages is the rules for where sopes begin and end, or some essoteric features like hoisting in JavaScript.


Where declarations are allowed

First, a big thanks to everyone who showed up for yesterday’s first Boldly Go Live stream. We had some audio problems, which I promise to fix before the next one. But otherwise it was a success, with some great discussion. You can catch the replay if you missed it. Here we are at the formal description of declaration syntax. Declarations and scope … Declaration = ConstDecl | TypeDecl | VarDecl .


Unbound identifiers

There are two identifiers that we can use in Go that do not create a binding to a thing… Declarations and scope … The blank identifier may be used like any other identifier in a declaration, but it does not introduce a binding and thus is not declared. In the package block, the identifier init may only be used for init function declarations, and like the blank identifier it does not introduce a new binding.


Declarations and scope

Declarations and scope A declaration binds a non-blank identifier to a constant, type, type parameter, variable, function, label, or package. Every identifier in a program must be declared. No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block. That’s a lot of words to basically say: You need to name your things. And your things need unique names.


Implicit blocks

On Monday we saw how to create explicit code blocks in Go. Now we’ll breeze through the list of implicit code blocks that exist. Blocks … In addition to explicit blocks in the source code, there are implicit blocks: The universe block encompasses all Go source text. Each package has a package block containing all Go source text for that package. Each file has a file block containing all Go source text in that file.