How does Context.Value(key interface{}) interface{} work? [duplicate]
This question already has an answer here:
What exactly does .(data_type) method called/do?
2 answers
Is this casting in golang?
1 answer
I do not understand this syntax
2 answers
What does piece of code do in golang? [duplicate]
1 answer
What is this “err.(*exec.ExitError)” thing in Go code?
2 answers
Please see the below code
import (
...
"context"
...
)
type myStruct struct {
ID string
Sig string
}
mySig := myStruct{
ID: "12345678",
Sig: "Secret_Signature_Token",
}
// Setting a Value associated with a Key in Context
_ := context.WithValue(ctx, "myKey", &mySig) -- 1
//Getting the same value
value, ok := ctx.Value("myKey").(*myStruct) -- 2
Now, my question is: what is the use/meaning of .(*myStruct)
in the above expression number 2
Could someone please explain the statement number 2 step by step.
go
marked as duplicate by Volker, JimB
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();
}
);
});
});
Nov 27 '18 at 14:13
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.
add a comment |
This question already has an answer here:
What exactly does .(data_type) method called/do?
2 answers
Is this casting in golang?
1 answer
I do not understand this syntax
2 answers
What does piece of code do in golang? [duplicate]
1 answer
What is this “err.(*exec.ExitError)” thing in Go code?
2 answers
Please see the below code
import (
...
"context"
...
)
type myStruct struct {
ID string
Sig string
}
mySig := myStruct{
ID: "12345678",
Sig: "Secret_Signature_Token",
}
// Setting a Value associated with a Key in Context
_ := context.WithValue(ctx, "myKey", &mySig) -- 1
//Getting the same value
value, ok := ctx.Value("myKey").(*myStruct) -- 2
Now, my question is: what is the use/meaning of .(*myStruct)
in the above expression number 2
Could someone please explain the statement number 2 step by step.
go
marked as duplicate by Volker, JimB
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();
}
);
});
});
Nov 27 '18 at 14:13
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.
1
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
Why are you ignoring the value returned bycontext.WithValue
?
– Flimzy
Nov 27 '18 at 12:34
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58
add a comment |
This question already has an answer here:
What exactly does .(data_type) method called/do?
2 answers
Is this casting in golang?
1 answer
I do not understand this syntax
2 answers
What does piece of code do in golang? [duplicate]
1 answer
What is this “err.(*exec.ExitError)” thing in Go code?
2 answers
Please see the below code
import (
...
"context"
...
)
type myStruct struct {
ID string
Sig string
}
mySig := myStruct{
ID: "12345678",
Sig: "Secret_Signature_Token",
}
// Setting a Value associated with a Key in Context
_ := context.WithValue(ctx, "myKey", &mySig) -- 1
//Getting the same value
value, ok := ctx.Value("myKey").(*myStruct) -- 2
Now, my question is: what is the use/meaning of .(*myStruct)
in the above expression number 2
Could someone please explain the statement number 2 step by step.
go
This question already has an answer here:
What exactly does .(data_type) method called/do?
2 answers
Is this casting in golang?
1 answer
I do not understand this syntax
2 answers
What does piece of code do in golang? [duplicate]
1 answer
What is this “err.(*exec.ExitError)” thing in Go code?
2 answers
Please see the below code
import (
...
"context"
...
)
type myStruct struct {
ID string
Sig string
}
mySig := myStruct{
ID: "12345678",
Sig: "Secret_Signature_Token",
}
// Setting a Value associated with a Key in Context
_ := context.WithValue(ctx, "myKey", &mySig) -- 1
//Getting the same value
value, ok := ctx.Value("myKey").(*myStruct) -- 2
Now, my question is: what is the use/meaning of .(*myStruct)
in the above expression number 2
Could someone please explain the statement number 2 step by step.
This question already has an answer here:
What exactly does .(data_type) method called/do?
2 answers
Is this casting in golang?
1 answer
I do not understand this syntax
2 answers
What does piece of code do in golang? [duplicate]
1 answer
What is this “err.(*exec.ExitError)” thing in Go code?
2 answers
go
go
edited Nov 27 '18 at 12:34
Flimzy
39k106597
39k106597
asked Nov 27 '18 at 11:47
Nitin SoniNitin Soni
13
13
marked as duplicate by Volker, JimB
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();
}
);
});
});
Nov 27 '18 at 14:13
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 Volker, JimB
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();
}
);
});
});
Nov 27 '18 at 14:13
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.
1
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
Why are you ignoring the value returned bycontext.WithValue
?
– Flimzy
Nov 27 '18 at 12:34
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58
add a comment |
1
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
Why are you ignoring the value returned bycontext.WithValue
?
– Flimzy
Nov 27 '18 at 12:34
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58
1
1
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
Why are you ignoring the value returned by
context.WithValue
?– Flimzy
Nov 27 '18 at 12:34
Why are you ignoring the value returned by
context.WithValue
?– Flimzy
Nov 27 '18 at 12:34
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58
add a comment |
1 Answer
1
active
oldest
votes
ctx.Value("myKey")
returns an interface. So .(*myStruct)
does a type assertion to convert it to the type *myStruct
. So the value
on the left hand side is of type *myStruct
and you can access its fields e.g. value.ID
.
A version of your example can be seen here: https://play.golang.org/p/Eg0v3vuSi6y
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
ctx.Value("myKey")
returns an interface. So .(*myStruct)
does a type assertion to convert it to the type *myStruct
. So the value
on the left hand side is of type *myStruct
and you can access its fields e.g. value.ID
.
A version of your example can be seen here: https://play.golang.org/p/Eg0v3vuSi6y
add a comment |
ctx.Value("myKey")
returns an interface. So .(*myStruct)
does a type assertion to convert it to the type *myStruct
. So the value
on the left hand side is of type *myStruct
and you can access its fields e.g. value.ID
.
A version of your example can be seen here: https://play.golang.org/p/Eg0v3vuSi6y
add a comment |
ctx.Value("myKey")
returns an interface. So .(*myStruct)
does a type assertion to convert it to the type *myStruct
. So the value
on the left hand side is of type *myStruct
and you can access its fields e.g. value.ID
.
A version of your example can be seen here: https://play.golang.org/p/Eg0v3vuSi6y
ctx.Value("myKey")
returns an interface. So .(*myStruct)
does a type assertion to convert it to the type *myStruct
. So the value
on the left hand side is of type *myStruct
and you can access its fields e.g. value.ID
.
A version of your example can be seen here: https://play.golang.org/p/Eg0v3vuSi6y
answered Nov 27 '18 at 12:04
RafaRafa
303211
303211
add a comment |
add a comment |
1
tour.golang.org/methods/15 . Redo the whole Tour.
– Volker
Nov 27 '18 at 12:11
Why are you ignoring the value returned by
context.WithValue
?– Flimzy
Nov 27 '18 at 12:34
@Fimzy, It is a sample code snippet. :)
– Nitin Soni
Nov 27 '18 at 13:58