英文:
Variable not declared, inside a if..else statement
问题
我刚开始学习Go语言,对于在Go语言中声明变量感到困惑。
例如,我在if...else语句中声明了req
和er
。
if strings.EqualFold(r.Method, "GET") || strings.EqualFold(r.Method, "") {
req, er := http.NewRequest(r.Method, r.Uri, b)
} else {
req, er := http.NewRequest(r.Method, r.Uri, b)
}
但是我从终端中得到了错误信息:
./goreq.go:127: req declared and not used
./goreq.go:127: er declared and not used
./goreq.go:129: req declared and not used
./goreq.go:129: er declared and not used
似乎我在if语句中声明的任何变量都没有起作用...我该如何解决这个问题?
英文:
I'm just started learning go lang, and I am confused about declaring variables in go lang
for example I've declare req
, er
inside if...else statement.
if strings.EqualFold(r.Method, "GET") || strings.EqualFold(r.Method, "") {
req, er := http.NewRequest(r.Method, r.Uri, b)
} else {
req, er := http.NewRequest(r.Method, r.Uri, b)
}
if er != nil {
// we couldn't parse the URL.
return nil, &Error{Err: er}
}
// add headers to the request
req.Host = r.Host
req.Header.Add("User-Agent", r.UserAgent)
req.Header.Add("Content-Type", r.ContentType)
req.Header.Add("Accept", r.Accept)
if r.headers != nil {
for _, header := range r.headers {
req.Header.Add(header.name, header.value)
}
}
But I've got error from terminal
./goreq.go:127: req declared and not used
./goreq.go:127: er declared and not used
./goreq.go:129: req declared and not used
./goreq.go:129: er declared and not used
seems like anything I declared inside If statement is not working... How can I solved it?
答案1
得分: 58
因为变量只在声明它们的作用域内定义:
package main
import "fmt"
func main() {
a := 1
fmt.Println(a)
{
a := 2
fmt.Println(a)
}
fmt.Println(a)
}
=
和 :=
的区别在于,=
只是赋值操作,而 :=
是变量声明和赋值的语法。
这行代码:
a := 1
等同于:
var a int
a = 1
你可能想要的是:
var req *http.Request
var er error
if strings.EqualFold(r.Method, "GET") || strings.EqualFold(r.Method, "") {
req, er = http.NewRequest(r.Method, r.Uri, b)
} else {
req, er = http.NewRequest(r.Method, r.Uri, b)
}
if er != nil {
// 无法解析 URL。
return nil, &Error{Err: er}
}
// 添加请求头
req.Host = r.Host
req.Header.Add("User-Agent", r.UserAgent)
req.Header.Add("Content-Type", r.ContentType)
req.Header.Add("Accept", r.Accept)
if r.headers != nil {
for _, header := range r.headers {
req.Header.Add(header.name, header.value)
}
}
英文:
Because variables are only defined in the scope in which they are declared:
package main
import "fmt"
func main() {
a := 1
fmt.Println(a)
{
a := 2
fmt.Println(a)
}
fmt.Println(a)
}
The difference between =
and :=
is that =
is just assignment and :=
is syntax for variable declaration and assigment
This:
a := 1
is equivalent to:
var a int
a = 1
What you probably want is:
var req *http.Request
var er error
if strings.EqualFold(r.Method, "GET") || strings.EqualFold(r.Method, "") {
req, er = http.NewRequest(r.Method, r.Uri, b)
} else {
req, er = http.NewRequest(r.Method, r.Uri, b)
}
if er != nil {
// we couldn't parse the URL.
return nil, &Error{Err: er}
}
// add headers to the request
req.Host = r.Host
req.Header.Add("User-Agent", r.UserAgent)
req.Header.Add("Content-Type", r.ContentType)
req.Header.Add("Accept", r.Accept)
if r.headers != nil {
for _, header := range r.headers {
req.Header.Add(header.name, header.value)
}
}
答案2
得分: 2
这是因为你在if else
条件语句内部声明了变量req
和er
,并且试图在作用域之外使用它们(作用域仅限于声明它们的if
和else
内部)。
你需要在if else
之外声明er
和req
。
英文:
This is because you have declared the variables req
and er
inside the if else
condition and are trying to use it outside the scope (the scope is just the if
and else
inside which they are declared).
You need to declare er
and req
outside the if else
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论