Reflect Type comparison
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
add a comment |
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
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
add a comment |
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
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
go reflection
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
.)
Thanks, I've read about difference between type and kind.
– Russiancold
Nov 12 at 13:51
add a comment |
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.
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%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
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
.)
Thanks, I've read about difference between type and kind.
– Russiancold
Nov 12 at 13:51
add a comment |
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
.)
Thanks, I've read about difference between type and kind.
– Russiancold
Nov 12 at 13:51
add a comment |
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
.)
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
.)
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 at 11:57
Vincenzo Maggio
3,1051939
3,1051939
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.
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.
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%2f53261277%2freflect-type-comparison%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
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