Go declared and not used logic [duplicate]











up vote
-3
down vote

favorite













This question already has an answer here:




  • Short variable declaration and “variable declared and not used” error

    2 answers



  • Why does golang compiler think the variable is declared but not used?

    1 answer



  • Go compiler says “declared and not used” but they are being used

    2 answers



  • using := gives unused error but using = don't in Go

    3 answers



  • “declared and not used” Error

    2 answers




Take this incredibly simple example. which shows variable assignment in and out of a block.



On compilation this results in: u declared and not used



var u string
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
}
log.Debug(u)


This simulates a logic block during which we might assess several things and set a var to the value we like depending on logic evaluation. How is this possible?










share|improve this question













marked as duplicate by JimB go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
    – JimB
    yesterday








  • 1




    The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
    – icza
    yesterday










  • Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
    – Elias Van Ootegem
    yesterday















up vote
-3
down vote

favorite













This question already has an answer here:




  • Short variable declaration and “variable declared and not used” error

    2 answers



  • Why does golang compiler think the variable is declared but not used?

    1 answer



  • Go compiler says “declared and not used” but they are being used

    2 answers



  • using := gives unused error but using = don't in Go

    3 answers



  • “declared and not used” Error

    2 answers




Take this incredibly simple example. which shows variable assignment in and out of a block.



On compilation this results in: u declared and not used



var u string
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
}
log.Debug(u)


This simulates a logic block during which we might assess several things and set a var to the value we like depending on logic evaluation. How is this possible?










share|improve this question













marked as duplicate by JimB go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
    – JimB
    yesterday








  • 1




    The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
    – icza
    yesterday










  • Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
    – Elias Van Ootegem
    yesterday













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite












This question already has an answer here:




  • Short variable declaration and “variable declared and not used” error

    2 answers



  • Why does golang compiler think the variable is declared but not used?

    1 answer



  • Go compiler says “declared and not used” but they are being used

    2 answers



  • using := gives unused error but using = don't in Go

    3 answers



  • “declared and not used” Error

    2 answers




Take this incredibly simple example. which shows variable assignment in and out of a block.



On compilation this results in: u declared and not used



var u string
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
}
log.Debug(u)


This simulates a logic block during which we might assess several things and set a var to the value we like depending on logic evaluation. How is this possible?










share|improve this question














This question already has an answer here:




  • Short variable declaration and “variable declared and not used” error

    2 answers



  • Why does golang compiler think the variable is declared but not used?

    1 answer



  • Go compiler says “declared and not used” but they are being used

    2 answers



  • using := gives unused error but using = don't in Go

    3 answers



  • “declared and not used” Error

    2 answers




Take this incredibly simple example. which shows variable assignment in and out of a block.



On compilation this results in: u declared and not used



var u string
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
}
log.Debug(u)


This simulates a logic block during which we might assess several things and set a var to the value we like depending on logic evaluation. How is this possible?





This question already has an answer here:




  • Short variable declaration and “variable declared and not used” error

    2 answers



  • Why does golang compiler think the variable is declared but not used?

    1 answer



  • Go compiler says “declared and not used” but they are being used

    2 answers



  • using := gives unused error but using = don't in Go

    3 answers



  • “declared and not used” Error

    2 answers








go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









David

9,75383458




9,75383458




marked as duplicate by JimB go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by JimB go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
    – JimB
    yesterday








  • 1




    The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
    – icza
    yesterday










  • Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
    – Elias Van Ootegem
    yesterday














  • 2




    Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
    – JimB
    yesterday








  • 1




    The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
    – icza
    yesterday










  • Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
    – Elias Van Ootegem
    yesterday








2




2




Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
– JimB
yesterday






Exactly as is says, the u declared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.
– JimB
yesterday






1




1




The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
– icza
yesterday




The short variable declaration will create new u and err variables, because u is not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.
– icza
yesterday












Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
– Elias Van Ootegem
yesterday




Can't believe nobody used the one sentence that clears everything up: u, err := is a declare and assign of both u and err inside a block, masking the var u string declared in a higher scope.
– Elias Van Ootegem
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Please pay attention that:



u, err := url.Parse("http://bing.com/search?q=dotnet")


in this line of code you have block scoped variable u which is differ from those one which declared in line: var u string, hence you have this error.

Moreover here you have shadowing for variable u from outer block scope which may lead to bugs, see this:



var u string
u = "blank"
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", u)
}
log.Printf("%#v", u)


Result will be:



2018/11/21 19:12:33 &url.URL{Scheme:"http", Opaque:"", User:(*url.Userinfo)(nil), Host:"bing.com", Path:"/search", RawPath:"", ForceQuery:false, RawQuery:"q=dotnet", Fragment:""}
2018/11/21 19:12:33 "blank"


As you can see here you have even different data types, and variable u not changed after this block.






share|improve this answer























  • Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
    – David
    17 hours ago


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Please pay attention that:



u, err := url.Parse("http://bing.com/search?q=dotnet")


in this line of code you have block scoped variable u which is differ from those one which declared in line: var u string, hence you have this error.

Moreover here you have shadowing for variable u from outer block scope which may lead to bugs, see this:



var u string
u = "blank"
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", u)
}
log.Printf("%#v", u)


Result will be:



2018/11/21 19:12:33 &url.URL{Scheme:"http", Opaque:"", User:(*url.Userinfo)(nil), Host:"bing.com", Path:"/search", RawPath:"", ForceQuery:false, RawQuery:"q=dotnet", Fragment:""}
2018/11/21 19:12:33 "blank"


As you can see here you have even different data types, and variable u not changed after this block.






share|improve this answer























  • Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
    – David
    17 hours ago















up vote
0
down vote













Please pay attention that:



u, err := url.Parse("http://bing.com/search?q=dotnet")


in this line of code you have block scoped variable u which is differ from those one which declared in line: var u string, hence you have this error.

Moreover here you have shadowing for variable u from outer block scope which may lead to bugs, see this:



var u string
u = "blank"
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", u)
}
log.Printf("%#v", u)


Result will be:



2018/11/21 19:12:33 &url.URL{Scheme:"http", Opaque:"", User:(*url.Userinfo)(nil), Host:"bing.com", Path:"/search", RawPath:"", ForceQuery:false, RawQuery:"q=dotnet", Fragment:""}
2018/11/21 19:12:33 "blank"


As you can see here you have even different data types, and variable u not changed after this block.






share|improve this answer























  • Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
    – David
    17 hours ago













up vote
0
down vote










up vote
0
down vote









Please pay attention that:



u, err := url.Parse("http://bing.com/search?q=dotnet")


in this line of code you have block scoped variable u which is differ from those one which declared in line: var u string, hence you have this error.

Moreover here you have shadowing for variable u from outer block scope which may lead to bugs, see this:



var u string
u = "blank"
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", u)
}
log.Printf("%#v", u)


Result will be:



2018/11/21 19:12:33 &url.URL{Scheme:"http", Opaque:"", User:(*url.Userinfo)(nil), Host:"bing.com", Path:"/search", RawPath:"", ForceQuery:false, RawQuery:"q=dotnet", Fragment:""}
2018/11/21 19:12:33 "blank"


As you can see here you have even different data types, and variable u not changed after this block.






share|improve this answer














Please pay attention that:



u, err := url.Parse("http://bing.com/search?q=dotnet")


in this line of code you have block scoped variable u which is differ from those one which declared in line: var u string, hence you have this error.

Moreover here you have shadowing for variable u from outer block scope which may lead to bugs, see this:



var u string
u = "blank"
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
log.Printf("%#v", u)
}
log.Printf("%#v", u)


Result will be:



2018/11/21 19:12:33 &url.URL{Scheme:"http", Opaque:"", User:(*url.Userinfo)(nil), Host:"bing.com", Path:"/search", RawPath:"", ForceQuery:false, RawQuery:"q=dotnet", Fragment:""}
2018/11/21 19:12:33 "blank"


As you can see here you have even different data types, and variable u not changed after this block.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Vladimir Kovpak

10.3k43646




10.3k43646












  • Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
    – David
    17 hours ago


















  • Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
    – David
    17 hours ago
















Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
– David
17 hours ago




Thanks for the comment. What still seems to escape me a little is how to access the set value of a variable set within a logic block. Take for example a simple - if { u="bob1"}else{ u = "bob2"} log.Printf(u)
– David
17 hours ago



Popular posts from this blog

Catalogne

Violoncelliste

Héron pourpré