In Go, I know that every new enclosing braces declare a new scope, this is true of all control flow statements. Today while writing a program, I stumbled in a scenario where I needed to use the select
statement like below
select {
case <-ctx.Done():
<-ch
return ctx.Err()
case resp := <-ch:
var b bytes.Buffer
if _, err := io.Copy(&b, resp.Body); err != nil {
return err
}
}
From the above code, I would like to access the b
variable after select statement is closed. Will that be possible?
I know I can move the logic that uses b
inside the select, but I would rather not go that route because select
is much complicated already on its own and want to keep it slim for easy understanding in future.