Here's the code on the playground to
experiment https://play.golang.org/p/GQG-g84nM2
I believe the problem is at line 9 (and 19, but line 9 is the one we
observe).
You are using ":=" (short variable declaration) to assign the newly created
data,
but that does not assign the variable in the function scope,
it declares a new variable, with the same name, in the if scope.
On Saturday, March 11, 2017 at 1:36:42 PM UTC+1, priyank pulumati wrote:
>
> Hello,
> Go newbie here
>
> func (data map[string] interface {}) {
> if len(data) == 0 {
> data := make(map[string]interface{})
> data["user"], _ = store.GetUser(context.Get(req, "userid").(string))
> } else {
> data["user"], _ = store.GetUser(context.Get(req, "userid").(string))
> }
> log.Println(data["user"])
> }
>
> This produces <nil> in console, where as,
>
>
> func (data map[string] interface {}) {
> if len(data) == 0 {
> data := make(map[string]interface{})
> data["user"], _ = store.GetUser(context.Get(req, "userid").(string))
> } else {
> data["user"], _ = store.GetUser(context.Get(req, "userid").(string))
> log.Println(data["user"])
> }
> }
>
> This produces the right value, why is it so ?
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.