Skip to main content
added 579 characters in body
Source Link
peterSO
  • 167.8k
  • 32
  • 303
  • 293

Read about blocks and declarations and scope in Go.

Each clause in a switch or select statement acts as an implicit block.

Blocks nest and influence scoping.

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

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.

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

Instead, write:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

Therefore, the artist, album, and year variables are no longer declared (and assigned) using short variable declarations inside the switch case clauses because that would hide the variable declarations in the outer block, they are merely assigned.

Read about blocks and declarations and scope in Go.

Each clause in a switch or select statement acts as an implicit block.

Blocks nest and influence scoping.

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

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.

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

Instead, write:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

Read about blocks and declarations and scope in Go.

Each clause in a switch or select statement acts as an implicit block.

Blocks nest and influence scoping.

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

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.

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

Instead, write:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

Therefore, the artist, album, and year variables are no longer declared (and assigned) using short variable declarations inside the switch case clauses because that would hide the variable declarations in the outer block, they are merely assigned.

Source Link
peterSO
  • 167.8k
  • 32
  • 303
  • 293

Read about blocks and declarations and scope in Go.

Each clause in a switch or select statement acts as an implicit block.

Blocks nest and influence scoping.

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

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.

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n")) 

The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

Instead, write:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))