Why does the compiler complain about an unused variable in this instance (when it is used by fmt.Fprintf)?
up vote
0
down vote
favorite
I have a simple piece of code where I want to convert elements of a slice into json and then print them out to my http.responseWriter.
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
However when I compile this I get the error "result declared and not used". If I add a simple line:
_ = result
Then everything compiles and works fine. Why does the compiler complain about this usage, and what is the correct way to do this in go?
Any insight is appreciated, my searches so far seem to indicate the call to Fprintf should count as a usage.
|
show 1 more comment
up vote
0
down vote
favorite
I have a simple piece of code where I want to convert elements of a slice into json and then print them out to my http.responseWriter.
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
However when I compile this I get the error "result declared and not used". If I add a simple line:
_ = result
Then everything compiles and works fine. Why does the compiler complain about this usage, and what is the correct way to do this in go?
Any insight is appreciated, my searches so far seem to indicate the call to Fprintf should count as a usage.
4
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
8
Most likely you have aresultvariable outside of theforloop, and since you use short variable declaration, that shadows the outerresult, and most likely the outerresultis the one that is never used, like in this example: Go Playground.
– icza
Nov 7 at 22:30
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
4
go vetwill catch this sort of problem.
– Michael Hampton
Nov 8 at 0:16
2
Don't use a JSON document as a format string. That's going to bite you sooner or later. Usefmt.Fprint(no f), or simplyw.Write(result)instead.
– Peter
Nov 8 at 7:16
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a simple piece of code where I want to convert elements of a slice into json and then print them out to my http.responseWriter.
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
However when I compile this I get the error "result declared and not used". If I add a simple line:
_ = result
Then everything compiles and works fine. Why does the compiler complain about this usage, and what is the correct way to do this in go?
Any insight is appreciated, my searches so far seem to indicate the call to Fprintf should count as a usage.
I have a simple piece of code where I want to convert elements of a slice into json and then print them out to my http.responseWriter.
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
However when I compile this I get the error "result declared and not used". If I add a simple line:
_ = result
Then everything compiles and works fine. Why does the compiler complain about this usage, and what is the correct way to do this in go?
Any insight is appreciated, my searches so far seem to indicate the call to Fprintf should count as a usage.
edited Nov 8 at 8:35
Flimzy
35.7k96395
35.7k96395
asked Nov 7 at 22:24
Chris
2,66721936
2,66721936
4
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
8
Most likely you have aresultvariable outside of theforloop, and since you use short variable declaration, that shadows the outerresult, and most likely the outerresultis the one that is never used, like in this example: Go Playground.
– icza
Nov 7 at 22:30
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
4
go vetwill catch this sort of problem.
– Michael Hampton
Nov 8 at 0:16
2
Don't use a JSON document as a format string. That's going to bite you sooner or later. Usefmt.Fprint(no f), or simplyw.Write(result)instead.
– Peter
Nov 8 at 7:16
|
show 1 more comment
4
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
8
Most likely you have aresultvariable outside of theforloop, and since you use short variable declaration, that shadows the outerresult, and most likely the outerresultis the one that is never used, like in this example: Go Playground.
– icza
Nov 7 at 22:30
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
4
go vetwill catch this sort of problem.
– Michael Hampton
Nov 8 at 0:16
2
Don't use a JSON document as a format string. That's going to bite you sooner or later. Usefmt.Fprint(no f), or simplyw.Write(result)instead.
– Peter
Nov 8 at 7:16
4
4
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
8
8
Most likely you have a
result variable outside of the for loop, and since you use short variable declaration, that shadows the outer result, and most likely the outer result is the one that is never used, like in this example: Go Playground.– icza
Nov 7 at 22:30
Most likely you have a
result variable outside of the for loop, and since you use short variable declaration, that shadows the outer result, and most likely the outer result is the one that is never used, like in this example: Go Playground.– icza
Nov 7 at 22:30
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
4
4
go vet will catch this sort of problem.– Michael Hampton
Nov 8 at 0:16
go vet will catch this sort of problem.– Michael Hampton
Nov 8 at 0:16
2
2
Don't use a JSON document as a format string. That's going to bite you sooner or later. Use
fmt.Fprint (no f), or simply w.Write(result) instead.– Peter
Nov 8 at 7:16
Don't use a JSON document as a format string. That's going to bite you sooner or later. Use
fmt.Fprint (no f), or simply w.Write(result) instead.– Peter
Nov 8 at 7:16
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result byte
customers := int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result byte
customers := int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
add a comment |
up vote
2
down vote
accepted
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result byte
customers := int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result byte
customers := int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
The code in question does not result in the error posted, for proof, check it on the Go Playground.
This error usually is (and the op confirmed it is too in this case) caused by having a local variable with same name outside of the block, and when using the short variable declaration, that shadows that variable.
This error can be reproduced with the following code:
var result byte
customers := int{}
w := os.Stdout
for _, element := range customers {
result, _ := json.Marshal(element)
fmt.Fprintf(w, string(result))
}
Attempting to compile and run it, we get the error (try it on the Go Playground):
prog.go:10:6: result declared and not used
Solution is to use a simple assignment instead of the short variable declaration if intention is to use the existing variable (in which case no new variable will be created), or use a different name for the variable if intention is not to use the outer, existing variable (but then the outer variable is to be removed or be used of course).
answered Nov 8 at 7:42
icza
156k22303343
156k22303343
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198800%2fwhy-does-the-compiler-complain-about-an-unused-variable-in-this-instance-when-i%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
The code you posted does not result in the error you get. For proof, see: Go Playground. Please post a Minimal, Complete, and Verifiable example.
– icza
Nov 7 at 22:28
8
Most likely you have a
resultvariable outside of theforloop, and since you use short variable declaration, that shadows the outerresult, and most likely the outerresultis the one that is never used, like in this example: Go Playground.– icza
Nov 7 at 22:30
Thanks @icza, that was it exactly. I didn't clock how variable declaration was working in go and it was the variable declared outside the scope that was causing the problem. Thanks!
– Chris
Nov 7 at 23:58
4
go vetwill catch this sort of problem.– Michael Hampton
Nov 8 at 0:16
2
Don't use a JSON document as a format string. That's going to bite you sooner or later. Use
fmt.Fprint(no f), or simplyw.Write(result)instead.– Peter
Nov 8 at 7:16