Non-standard variable name

Multi tool use
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
add a comment |
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
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 amap[string]string
which is whathttp.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
add a comment |
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
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
go
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 amap[string]string
which is whathttp.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
add a comment |
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 amap[string]string
which is whathttp.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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 16 '18 at 5:38
answered Nov 16 '18 at 5:29


Cerise LimónCerise Limón
54.4k56991
54.4k56991
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Kknzex66Zb1,Fb0sAWsY hBwhSmUdCZoiN6dIn6L,JJ9Qi hYQnpY v5 J D0
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 whathttp.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