FAQ
Hello,
I have this nested map with values of type interface{}

*package main*

*import "fmt"*

*func main() {*
* templates := map[string]interface{}{*
* "index":"index.html",*
* "specs": map[string]interface{}{*
* "add":"add.html",*
* "edit": "edit.html",*
* },*
* }*
* fmt.Println(templates["specs"]["add"])*
*}*


but when I want access a "add" inside "specs": prog.go:17: invalid operation: templates["specs"]["add"] (index of type interface {})


I understand that I should convert type interface to the type correct. but, how in a type of map type?


I tried this:


*var s interface{} = "add"*

*fmt.Println(templates["specs"][s])*


but not work...


Best,

Emilio

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Mar 23, 2014 at 3:32 pm
    On Sun, Mar 23, 2014 at 4:16 PM, Emilio García-Pumarino Álvarez wrote:
    http://play.golang.org/p/D2cz2Af4vE

    -j

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Emilio García-Pumarino Álvarez at Mar 23, 2014 at 3:36 pm
    Thanks so much!! :)

    El domingo, 23 de marzo de 2014 15:31:21 UTC, Jan Mercl escribió:
    On Sun, Mar 23, 2014 at 4:16 PM, Emilio García-Pumarino Álvarez
    <emil...@gmail.com <javascript:>> wrote:

    http://play.golang.org/p/D2cz2Af4vE

    -j
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at Mar 23, 2014 at 3:40 pm

    On Sun, Mar 23, 2014 at 08:16:55AM -0700, Emilio García-Pumarino Álvarez wrote:
    *package main*

    *import "fmt"*

    *func main() {*
    * templates := map[string]interface{}{*
    * "index":"index.html",*
    * "specs": map[string]interface{}{*
    * "add":"add.html",*
    * "edit": "edit.html",*
    * },*
    * }*
    * fmt.Println(templates["specs"]["add"])*
    *}*


    but when I want access a "add" inside "specs": prog.go:17: invalid operation: templates["specs"]["add"] (index of type interface {})


    I understand that I should convert type interface to the type correct. but, how in a type of map type?
    It's a two step process; you have to convert the value of templates["specs"],
    an interface{}, to map[string]interface{} in order to use it.

         http://play.golang.org/p/C5KF3JBzMz

    A couple of comments.

    Please try to use the playground for code snippets. It makes it easier to help. Having to decipher and edit
    a jumbled mass of code impedes the process.

    You may want to consider a simpler structure. This looks like hierarchical web
    stuff, so why not use a map[string]string with slash separated keys: "specs/add", "specs/edit", etc?
    You have to go through all the levels anyway.

    -Gyepi

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Emilio García-Pumarino Álvarez at Mar 23, 2014 at 4:25 pm
    Ok. From now I will use Go Playground.

    About the use a map[string]string is a very good idea, I did not know that
    could use special characters in the key of a map... I will use this
    pattern. Thanks!

    Best,
    Emilio

    El domingo, 23 de marzo de 2014 15:40:11 UTC, Gyepi SAM escribió:
    On Sun, Mar 23, 2014 at 08:16:55AM -0700, Emilio García-Pumarino Álvarez
    wrote:
    *package main*

    *import "fmt"*

    *func main() {*
    * templates := map[string]interface{}{*
    * "index":"index.html",*
    * "specs": map[string]interface{}{*
    * "add":"add.html",*
    * "edit": "edit.html",*
    * },*
    * }*
    * fmt.Println(templates["specs"]["add"])*
    *}*


    but when I want access a "add" inside "specs": prog.go:17: invalid
    operation: templates["specs"]["add"] (index of type interface {})

    I understand that I should convert type interface to the type correct.
    but, how in a type of map type?

    It's a two step process; you have to convert the value of
    templates["specs"],
    an interface{}, to map[string]interface{} in order to use it.

    http://play.golang.org/p/C5KF3JBzMz

    A couple of comments.

    Please try to use the playground for code snippets. It makes it easier to
    help. Having to decipher and edit
    a jumbled mass of code impedes the process.

    You may want to consider a simpler structure. This looks like hierarchical
    web
    stuff, so why not use a map[string]string with slash separated keys:
    "specs/add", "specs/edit", etc?
    You have to go through all the levels anyway.

    -Gyepi
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Adie Nurahmadie at Mar 23, 2014 at 4:15 pm
    Something like this? http://play.golang.org/p/xK968dWxWx

    On Sun, Mar 23, 2014 at 10:16 PM, Emilio García-Pumarino Álvarez wrote:

    Hello,
    I have this nested map with values of type interface{}

    *package main*

    *import "fmt"*

    *func main() {*
    * templates := map[string]interface{}{*
    * "index":"index.html",*
    * "specs": map[string]interface{}{*
    * "add":"add.html",*
    * "edit": "edit.html",*
    * },*
    * }*
    * fmt.Println(templates["specs"]["add"])*
    *}*


    but when I want access a "add" inside "specs": prog.go:17: invalid operation: templates["specs"]["add"] (index of type interface {})


    I understand that I should convert type interface to the type correct. but, how in a type of map type?


    I tried this:


    *var s interface{} = "add"*

    *fmt.Println(templates["specs"][s])*


    but not work...


    Best,

    Emilio

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    regards,
    Nurahmadie
    --

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 23, '14 at 3:17p
activeMar 23, '14 at 4:25p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase