Hi,

I've wrote about the theoretical side of that here:
https://blog.merovius.de/2018/06/03/why-doesnt-go-have-variance-in.html
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.

On Sun, Feb 23, 2020 at 10:24 AM Glen Huang <hey....@gmail.com> wrote:

> 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.
>
> On Sunday, February 23, 2020 at 5:12:27 PM UTC+8, Glen Huang wrote:
>>
>> 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
>>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/golang-nuts/3bd425dd-1ca4-4bb3-8385-07c3aece5912%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG6T7fkvY3Sdh7wViiOnLBBwWuSUpM%2BrEz2Lq%2B1uVLH9Q%40mail.gmail.com.

Reply via email to