Directly assign map[string]int to map[string]interface{}?

96 views
Skip to first unread message

Glen Huang

unread,
Feb 23, 2020, 4:12:27 AM2/23/20
to golang-nuts
Hi,

I have a function that accepts an argument of type map[string]interface{}, and I also have a value of  type map[string]int.

Currently it seems I can't directly pass the value to the function. Is there anyway I can directly coerce it or a new value of the exact matching type must be created?

I find it quite surprising that you can directly assign any variables to interface{} but not when they are both "scoped inside" a map or a slice. Is the asymmetry by design? 

Regards

Glen Huang

unread,
Feb 23, 2020, 4:24:11 AM2/23/20
to golang-nuts
Another slightly related topic:

switch v := i.(type) {
case map[string]interface{}, map[string]int:
    fmt.Print(v)
}

Ideally v is of type map[string]interface{}, but it's interface{} instead.

Axel Wagner

unread,
Feb 23, 2020, 4:37:56 AM2/23/20
to Glen Huang, golang-nuts
Hi,

I've wrote about the theoretical side of that here:
The post uses slices, but the same arguments apply to maps as well. Specifically, while it *may* seem like you should be able to use a map[string]int as a map[string]interface{}, if you *could*, you would also have to explain what happens when you write something to it that is not an int. i.e. how would this code behave: https://play.golang.org/p/StJrbhmjCHh

There really is no good answer to this. So as long as the type allows both reading and writing, making map variant really isn't possible.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3bd425dd-1ca4-4bb3-8385-07c3aece5912%40googlegroups.com.

Dan Kortschak

unread,
Feb 23, 2020, 4:41:31 AM2/23/20
to golan...@googlegroups.com
Have a read of https://research.swtch.com/interfaces. There you'll see
that the memory layout of int and interface{} are not the same. This
means you can't just treat one as the other, which essentially is what
you are asking for.
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/39f21b5a-12f1-4c9b-a64c-19bf98c24919%40googlegroups.com
> .


Dan Kortschak

unread,
Feb 23, 2020, 5:00:30 AM2/23/20
to golang-nuts
Go is a statically typed language, but the time you get into the case,
you must know the concrete type of the v. You allows it to be either
map[string]interface{} or map[string]int, this is not a single known
type, so the original input type (interface{}) is used.
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/3bd425dd-1ca4-4bb3-8385-07c3aece5912%40googlegroups.com
> .


Glen Huang

unread,
Feb 23, 2020, 5:04:27 AM2/23/20
to golang-nuts
This is a great write-up! Going from explaining the terms to applying the terms to the actual problems.

I learned a lot. Thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Volker Dobler

unread,
Feb 23, 2020, 7:12:10 AM2/23/20
to golang-nuts
It boils down to the meaning of interface{}.
interface{} means interface{} and _not_ "any type".
While you can assign anything to a variable of type interface{}
this does not mean that a variable of type interface{} _is_
"any type". There is no "any type" type in Go.

V.
Reply all
Reply to author
Forward
0 new messages