The builtin make()
function creates a non-pointer value of your MyType
map type, yet the return type is a pointer. That's what the error message tells if you try to compile it:
cannot use make(MyType) (type MyType) as type *MyType in return argument
If you return a pointer to the value, it works:
type MyType map[string]string
func NewMyType() *MyType {
m := make(MyType)
return &m
}
If you would want to use a single line for it, you could use a composite literal:
func NewMyType() *MyType {
return &MyType{}
}
But maps (map values) are already implement as pointers in the background, so this is redundant and unnecessary. Just return the map-value as-is:
type MyType map[string]string
func NewMyType() MyType {
return make(MyType)
}
Or with a composite literal:
func NewMyType() MyType {
return MyType{}
}
Although "constructors" for such simple types (simple creation) are not necessary, unless you want to do other things before returning it (e.g. specify its initial capacity or fill it with initial values).