switch statement "<var> declared and not used"

1,691 views
Skip to first unread message

Bobby Bingham

unread,
Jun 5, 2011, 5:44:55 PM6/5/11
to golang-nuts
I'm new to Go, so forgive me if this question has an obvious answer,
or if this isn't the right place to ask.

I'm trying to parse a file, where part of the file is a list of
strings. I have created a function readString which reads an
individual string, and returns both the string and the input buffer,
minus whatever data was consumed. When there are no more strings to
read, that is signaled by the returned string being the empty string.

Then I have this function to read the entire list of strings:

func readStringList(buf []byte) ([]string, []byte) {
var ret []string

loop: for {
switch str, buf := readString(buf); {
case len(str) > 0:
ret = append(ret, str)
default:
break loop
}
}

return ret, buf
}

This gives me the error "buf declared and not used" on the line
containing the switch statement. It's pretty trivial to work around
by assigning to a temporary variable, and then assigning that to buf
within each case, but I don't understand why it's an error in the
first place.

I haven't delved into the part of the spec regarding switch
statements, but the part about short variable declarations pretty
clearly says that this will not create a new variable buf, but rather
assign the existing buf variable a new value. And that value will be
used in the next iteration of the loop, or in the return statement.

I get the same result if I use an if/else instead of a switch.

Can some body shed some light on why this is an error?

Thanks,
Bobby

Alexey Gokhberg

unread,
Jun 5, 2011, 5:58:16 PM6/5/11
to golang-nuts
According to the language specification:

" ... a short variable declaration may redeclare variables provided
they were originally declared in the same block with the same
type ..."

The variable "buf" was originally declared not in the same but in the
outer block, thus the fragment

str, buf := readString(buf);

does not redeclare the original "buf" but declares a new variable
"buf" in the inner block.

Alexey Gokhberg

unread,
Jun 5, 2011, 6:12:40 PM6/5/11
to golang-nuts
P.S. But now I have another question.

Let's consider a simple program:

package main

func main() {
var x int32 = 1
{
var x int32 = x + 1
println(x)
}
println(x)
}

It will output:

2
1

This means that in the statement

var x int32 = x + 1

"x" on the right side stands for the variable declared in the outer
scope and that Go compiler first compiles the initialization
expression and then declares the inner "x".

Where does Go specification state that compilation must be done in
this order?

roger peppe

unread,
Jun 5, 2011, 6:24:07 PM6/5/11
to Alexey Gokhberg, golang-nuts
On 5 June 2011 23:12, Alexey Gokhberg

in the Go specification, under "Declarations and Scope", it states:

``The scope of a constant or variable identifier declared inside a
function begins at the end of the ConstSpec or VarSpec and ends at the
end of the innermost containing block.''

that is, the scope of the x in the second var declaration starts after that
var declaration, hence x in the assigned expression refers to the previously
declared x.

Alexey Gokhberg

unread,
Jun 5, 2011, 6:42:12 PM6/5/11
to golang-nuts
Oh, I see. Thanks.

On 6 Jun., 00:24, roger peppe <rogpe...@gmail.com> wrote:
>
> in the Go specification, under "Declarations and Scope", it states:
>
> ``The scope of a constant or variable identifier declared inside a
> function begins at the end of the ConstSpec or VarSpec and ends at the
> end of the innermost containing block.''
>
> that is, the scope of the x in the second var declaration starts after that
> var declaration, hence x in the assigned expression refers to the previously
> declared x.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
Reply all
Reply to author
Forward
0 new messages