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?
go, but it seems like if you declarereqanderbefore the if statement and assign them inside the if statement, then the warnings (are they warnings or actually errors?) should go away. - jonhopkinserrrather thaner- Ari Seyhun