How does Context.Value(key interface{}) interface{} work? [duplicate]












-2
















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.










share|improve this question















marked as duplicate by Volker, 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();
}
);
});
});
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 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
















-2
















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.










share|improve this question















marked as duplicate by Volker, 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();
}
);
});
});
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 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














-2












-2








-2









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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();
}
);
});
});
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 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();
}
);
});
});
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 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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






    share|improve this answer




























      1














      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






      share|improve this answer


























        1












        1








        1







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 12:04









        RafaRafa

        303211




        303211

















            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)