我正在尝试理解Golang中的简单Switch语句,但是我卡在了变量作用域上。
var body string
var errs error
req := gorequest.New()
var resp gorequest.Response
switch verb {
case 0:
resp, body, errs := req.Get(suburl).
Set("X-Auth-Token", d.Token).
Set("Content-type", "application/json").
End()
}
if errs != nil {
return &ConnResponse{resp.StatusCode, body, fmt.Errorf("%s", errs)}
}
我已经在switch语句之前声明了resp、body、errs和req,我原本以为它们会在switch语句体之后可用。 但编译器返回了以下错误信息(来自case声明)。
# command-line-arguments
./conn.go:135:3: resp declared and not used
./conn.go:135:9: body declared and not used
./conn.go:135:15: errs declared and not used
我很好奇,switch语句体内的变量作用域是否与在函数中声明的不同?如何修改这段代码才能够在switch语句体后获取数据?