在 if..else 语句中,变量未声明。

huangapple go评论93阅读模式
英文:

Variable not declared, inside a if..else statement

问题

我刚开始学习Go语言,对于在Go语言中声明变量感到困惑。

例如,我在if...else语句中声明了reqer

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)
}

go play

=:= 的区别在于,= 只是赋值操作,而 := 是变量声明和赋值的语法。

这行代码:

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)
}

go play

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条件语句内部声明了变量reqer,并且试图在作用域之外使用它们(作用域仅限于声明它们的ifelse内部)。

你需要在if else之外声明erreq

英文:

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

huangapple
  • 本文由 发表于 2014年1月31日 21:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/21481288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定