Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable not declared, inside a if..else statement

Tags:

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?

like image 695
LiJung Avatar asked Jan 31 '14 13:01

LiJung


People also ask

Can we declare variable in if else?

Java allows you to declare variables within the body of a while or if statement, but it's important to remember the following: A variable is available only from its declaration down to the end of the braces in which it is declared.

Can you assign a variable inside an if statement?

Yes, you can assign the value of variable inside if. I wouldn't recommend it. The problem is, it looks like a common error where you try to compare values, but use a single = instead of == or === . @EddieB This isn't like shooting yourself in the foot.

Can you declare variables in an if statement C?

No. That is not allowed in C. According to the ANSI C Grammar, the grammar for the if statement is IF '(' expression ')' statement . expression cannot resolve to declaration , so there is no way to put a declaration in an if statement like in your example.


1 Answers

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)     } } 
like image 69
Arjan Avatar answered Oct 01 '22 20:10

Arjan