Some blatherings by Keith Ball

View my projects on GitHub

Go Gotcha

01 Mar 2016

Go is lexically scoped…

What is wrong with the following code?

// Item
var item *Item

itemID := u.Query().Get("itemID")

if itemID != "" {
item, err := repository.GetItem(itemID)

log.Printf("The item %v", item)

The code shows how Item is being redeclared in the scope of the if.

Further reading here: Declarations and scope