The concept of shadowing should be familiar to many readers. But here we see how Go defines it…
Declarations and scope
…
An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration.
Notice that shadowing is not limited to variables. You can shadow function names, types, constants, or variables.
Let’s see some examples.
func main() {
main := "世界" // Shadows the name of the main function
fmt.Println("Hello,", main)
}
import "fmt"
func main() {
fmt := "世界" // Shadows the name of the imported package
fmt.Println("Hello,", fmt) // Now this won't compile, because `fmt` is a var, not a package
}
Quotes from The Go Programming Language Specification Version of December 15, 2022