Reflect Type comparison










-1














I want to ensure that the type of map keys is string. Key() method returns Type and I'm not sure what is the right way to check if it's string. The only thing came to my mind is:



if v.Type().Key() == reflect.TypeOf("") 
fmt.Print("It is string")



Is it the right way?










share|improve this question

















  • 1




    Are you looking for Kind?
    – Volker
    Nov 12 at 11:45










  • @Volker As I said, I just want to ensure that key is a string
    – Russiancold
    Nov 12 at 11:50
















-1














I want to ensure that the type of map keys is string. Key() method returns Type and I'm not sure what is the right way to check if it's string. The only thing came to my mind is:



if v.Type().Key() == reflect.TypeOf("") 
fmt.Print("It is string")



Is it the right way?










share|improve this question

















  • 1




    Are you looking for Kind?
    – Volker
    Nov 12 at 11:45










  • @Volker As I said, I just want to ensure that key is a string
    – Russiancold
    Nov 12 at 11:50














-1












-1








-1







I want to ensure that the type of map keys is string. Key() method returns Type and I'm not sure what is the right way to check if it's string. The only thing came to my mind is:



if v.Type().Key() == reflect.TypeOf("") 
fmt.Print("It is string")



Is it the right way?










share|improve this question













I want to ensure that the type of map keys is string. Key() method returns Type and I'm not sure what is the right way to check if it's string. The only thing came to my mind is:



if v.Type().Key() == reflect.TypeOf("") 
fmt.Print("It is string")



Is it the right way?







go reflection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 11:32









Russiancold

691315




691315







  • 1




    Are you looking for Kind?
    – Volker
    Nov 12 at 11:45










  • @Volker As I said, I just want to ensure that key is a string
    – Russiancold
    Nov 12 at 11:50













  • 1




    Are you looking for Kind?
    – Volker
    Nov 12 at 11:45










  • @Volker As I said, I just want to ensure that key is a string
    – Russiancold
    Nov 12 at 11:50








1




1




Are you looking for Kind?
– Volker
Nov 12 at 11:45




Are you looking for Kind?
– Volker
Nov 12 at 11:45












@Volker As I said, I just want to ensure that key is a string
– Russiancold
Nov 12 at 11:50





@Volker As I said, I just want to ensure that key is a string
– Russiancold
Nov 12 at 11:50













2 Answers
2






active

oldest

votes


















4














Yes, what you did reports if the key type is "exactly" string.



But for example if the key type would be a custom type having string as its underlying type, like in this example:



type mystr string
m := map[mystr]int


Then the key type would not be equal to reflect.TypeOf("").



It's up to you if this is what you want. If you do want to accept the above map types too, you may check the kind of the key if it equals to reflect.String like this:



if v.Type().Key() == reflect.TypeOf("") 
fmt.Print("It is string")


if v.Type().Key().Kind() == reflect.String
fmt.Print("It is string kind")



For the above map[mystr]int, this is the output (try it on the Go Playground):



It is string kind


(The key is not of type string, but it is of kind string.)






share|improve this answer






















  • Thanks, I've read about difference between type and kind.
    – Russiancold
    Nov 12 at 13:51


















1














You can extract the Kind of the key and confront it with kind enumerations in reflect package like reflect.String as in:



package main

import (
"fmt"
"reflect"
)

func main()

obj := make(map[string]interface)

fmt.Println(reflect.TypeOf(obj).Key().Kind() == reflect.String) // It will print true



See this Go Playground snippet if you want to try it.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261277%2freflect-type-comparison%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Yes, what you did reports if the key type is "exactly" string.



    But for example if the key type would be a custom type having string as its underlying type, like in this example:



    type mystr string
    m := map[mystr]int


    Then the key type would not be equal to reflect.TypeOf("").



    It's up to you if this is what you want. If you do want to accept the above map types too, you may check the kind of the key if it equals to reflect.String like this:



    if v.Type().Key() == reflect.TypeOf("") 
    fmt.Print("It is string")


    if v.Type().Key().Kind() == reflect.String
    fmt.Print("It is string kind")



    For the above map[mystr]int, this is the output (try it on the Go Playground):



    It is string kind


    (The key is not of type string, but it is of kind string.)






    share|improve this answer






















    • Thanks, I've read about difference between type and kind.
      – Russiancold
      Nov 12 at 13:51















    4














    Yes, what you did reports if the key type is "exactly" string.



    But for example if the key type would be a custom type having string as its underlying type, like in this example:



    type mystr string
    m := map[mystr]int


    Then the key type would not be equal to reflect.TypeOf("").



    It's up to you if this is what you want. If you do want to accept the above map types too, you may check the kind of the key if it equals to reflect.String like this:



    if v.Type().Key() == reflect.TypeOf("") 
    fmt.Print("It is string")


    if v.Type().Key().Kind() == reflect.String
    fmt.Print("It is string kind")



    For the above map[mystr]int, this is the output (try it on the Go Playground):



    It is string kind


    (The key is not of type string, but it is of kind string.)






    share|improve this answer






















    • Thanks, I've read about difference between type and kind.
      – Russiancold
      Nov 12 at 13:51













    4












    4








    4






    Yes, what you did reports if the key type is "exactly" string.



    But for example if the key type would be a custom type having string as its underlying type, like in this example:



    type mystr string
    m := map[mystr]int


    Then the key type would not be equal to reflect.TypeOf("").



    It's up to you if this is what you want. If you do want to accept the above map types too, you may check the kind of the key if it equals to reflect.String like this:



    if v.Type().Key() == reflect.TypeOf("") 
    fmt.Print("It is string")


    if v.Type().Key().Kind() == reflect.String
    fmt.Print("It is string kind")



    For the above map[mystr]int, this is the output (try it on the Go Playground):



    It is string kind


    (The key is not of type string, but it is of kind string.)






    share|improve this answer














    Yes, what you did reports if the key type is "exactly" string.



    But for example if the key type would be a custom type having string as its underlying type, like in this example:



    type mystr string
    m := map[mystr]int


    Then the key type would not be equal to reflect.TypeOf("").



    It's up to you if this is what you want. If you do want to accept the above map types too, you may check the kind of the key if it equals to reflect.String like this:



    if v.Type().Key() == reflect.TypeOf("") 
    fmt.Print("It is string")


    if v.Type().Key().Kind() == reflect.String
    fmt.Print("It is string kind")



    For the above map[mystr]int, this is the output (try it on the Go Playground):



    It is string kind


    (The key is not of type string, but it is of kind string.)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 13:51

























    answered Nov 12 at 11:59









    icza

    161k24314354




    161k24314354











    • Thanks, I've read about difference between type and kind.
      – Russiancold
      Nov 12 at 13:51
















    • Thanks, I've read about difference between type and kind.
      – Russiancold
      Nov 12 at 13:51















    Thanks, I've read about difference between type and kind.
    – Russiancold
    Nov 12 at 13:51




    Thanks, I've read about difference between type and kind.
    – Russiancold
    Nov 12 at 13:51













    1














    You can extract the Kind of the key and confront it with kind enumerations in reflect package like reflect.String as in:



    package main

    import (
    "fmt"
    "reflect"
    )

    func main()

    obj := make(map[string]interface)

    fmt.Println(reflect.TypeOf(obj).Key().Kind() == reflect.String) // It will print true



    See this Go Playground snippet if you want to try it.






    share|improve this answer

























      1














      You can extract the Kind of the key and confront it with kind enumerations in reflect package like reflect.String as in:



      package main

      import (
      "fmt"
      "reflect"
      )

      func main()

      obj := make(map[string]interface)

      fmt.Println(reflect.TypeOf(obj).Key().Kind() == reflect.String) // It will print true



      See this Go Playground snippet if you want to try it.






      share|improve this answer























        1












        1








        1






        You can extract the Kind of the key and confront it with kind enumerations in reflect package like reflect.String as in:



        package main

        import (
        "fmt"
        "reflect"
        )

        func main()

        obj := make(map[string]interface)

        fmt.Println(reflect.TypeOf(obj).Key().Kind() == reflect.String) // It will print true



        See this Go Playground snippet if you want to try it.






        share|improve this answer












        You can extract the Kind of the key and confront it with kind enumerations in reflect package like reflect.String as in:



        package main

        import (
        "fmt"
        "reflect"
        )

        func main()

        obj := make(map[string]interface)

        fmt.Println(reflect.TypeOf(obj).Key().Kind() == reflect.String) // It will print true



        See this Go Playground snippet if you want to try it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 11:57









        Vincenzo Maggio

        3,1051939




        3,1051939



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261277%2freflect-type-comparison%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党