在Golang中使用派生地图类型索引的正确方法

It could be a beginner's question. Code is like,

type MYMAP map[int]int

func (o *MYMAP) dosth(){
    //this will fail to compile
    o[1]=2
}

error message: invalid operation: o[1] (index of type *MYMAP)

How to access the underlying type of MYMAP as map?

The problem isn't that it's an alias, it's that it's a pointer to a map.

Go will not automatically deference pointers for map or slice access the way it will for method calls. Replacing o[1]=2 with (*o)[1]=2 will work. Though you should consider why you're (effectively) using a pointer to a map. There can be good reasons to do this, but usually you don't need a pointer to a map since maps are "reference types", meaning that you don't need a pointer to them to see the side effects of mutating them across the program.

An easy fix could be done by getting rid of pointer, just change o *MYMAP to o MYMAP

type MYMAP map[int]int

func (o MYMAP) dosth(){
    o[1]=2
}