Non-standard variable name










-1















Say I want to represent HTTP headers with a struct, something like:



 Headers struct 
'x-requested-by' 'foo'



it definitely does not like a variable name with hyphens, etc. Any way around this?










share|improve this question

















  • 2





    Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

    – Cerise Limón
    Nov 16 '18 at 5:12











  • @ThunderCat can you show an example of that? will upvote thx

    – Olegzandr Denman
    Nov 16 '18 at 5:26






  • 2





    Or you could use a map[string]string which is what http.Header is in the standard library.

    – Joel Cornett
    Nov 16 '18 at 5:33











  • Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

    – Adrian
    Nov 16 '18 at 15:22















-1















Say I want to represent HTTP headers with a struct, something like:



 Headers struct 
'x-requested-by' 'foo'



it definitely does not like a variable name with hyphens, etc. Any way around this?










share|improve this question

















  • 2





    Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

    – Cerise Limón
    Nov 16 '18 at 5:12











  • @ThunderCat can you show an example of that? will upvote thx

    – Olegzandr Denman
    Nov 16 '18 at 5:26






  • 2





    Or you could use a map[string]string which is what http.Header is in the standard library.

    – Joel Cornett
    Nov 16 '18 at 5:33











  • Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

    – Adrian
    Nov 16 '18 at 15:22













-1












-1








-1








Say I want to represent HTTP headers with a struct, something like:



 Headers struct 
'x-requested-by' 'foo'



it definitely does not like a variable name with hyphens, etc. Any way around this?










share|improve this question














Say I want to represent HTTP headers with a struct, something like:



 Headers struct 
'x-requested-by' 'foo'



it definitely does not like a variable name with hyphens, etc. Any way around this?







go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 4:53









Alexander MillsAlexander Mills

20k35163346




20k35163346







  • 2





    Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

    – Cerise Limón
    Nov 16 '18 at 5:12











  • @ThunderCat can you show an example of that? will upvote thx

    – Olegzandr Denman
    Nov 16 '18 at 5:26






  • 2





    Or you could use a map[string]string which is what http.Header is in the standard library.

    – Joel Cornett
    Nov 16 '18 at 5:33











  • Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

    – Adrian
    Nov 16 '18 at 15:22












  • 2





    Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

    – Cerise Limón
    Nov 16 '18 at 5:12











  • @ThunderCat can you show an example of that? will upvote thx

    – Olegzandr Denman
    Nov 16 '18 at 5:26






  • 2





    Or you could use a map[string]string which is what http.Header is in the standard library.

    – Joel Cornett
    Nov 16 '18 at 5:33











  • Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

    – Adrian
    Nov 16 '18 at 15:22







2




2





Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

– Cerise Limón
Nov 16 '18 at 5:12





Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire.

– Cerise Limón
Nov 16 '18 at 5:12













@ThunderCat can you show an example of that? will upvote thx

– Olegzandr Denman
Nov 16 '18 at 5:26





@ThunderCat can you show an example of that? will upvote thx

– Olegzandr Denman
Nov 16 '18 at 5:26




2




2





Or you could use a map[string]string which is what http.Header is in the standard library.

– Joel Cornett
Nov 16 '18 at 5:33





Or you could use a map[string]string which is what http.Header is in the standard library.

– Joel Cornett
Nov 16 '18 at 5:33













Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

– Adrian
Nov 16 '18 at 15:22





Using a struct for headers is going to be pretty rough anyway. The set of headers you can handle will be fixed at compile-time, you'll be allocating for headers whether or not they're present, and you won't be able to iterate them. I would strongly recommend just using a map like the stdlib (and every other HTTP lib I've ever seen) does.

– Adrian
Nov 16 '18 at 15:22












1 Answer
1






active

oldest

votes


















1














Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire. See the encoding/json package for an example of how to do this.



If the higher-level problem is storing key-value pairs with arbitrary string keys, then use a map.






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%2f53331653%2fnon-standard-variable-name%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire. See the encoding/json package for an example of how to do this.



    If the higher-level problem is storing key-value pairs with arbitrary string keys, then use a map.






    share|improve this answer





























      1














      Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire. See the encoding/json package for an example of how to do this.



      If the higher-level problem is storing key-value pairs with arbitrary string keys, then use a map.






      share|improve this answer



























        1












        1








        1







        Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire. See the encoding/json package for an example of how to do this.



        If the higher-level problem is storing key-value pairs with arbitrary string keys, then use a map.






        share|improve this answer















        Specify actual header names as field tags and use those field tags when encoding or decoding the struct to the wire. See the encoding/json package for an example of how to do this.



        If the higher-level problem is storing key-value pairs with arbitrary string keys, then use a map.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 5:38

























        answered Nov 16 '18 at 5:29









        Cerise LimónCerise Limón

        54.4k56991




        54.4k56991





























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331653%2fnon-standard-variable-name%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

            政党