This question already has an answer here:
I have underlying type of string: type Capability string
.
I wanted to use it as a string inside map of strings but I am getting an error:
cannot use cap (type Capability) as type string in map index
This is my code:
package main
import (
"fmt"
)
type Capability string
var caps_list = map[string]int {
"HOME" : 1,
}
func main() {
var cap Capability // string
cap = "HOME"
fmt.Print(string(caps_list[cap]))
}
Why it doesn't accept it ? it is a string after all.
You can try my code here:
https://play.golang.org/p/r-h9Hu8_eoM
</div>
Just need to change to use string
on the cap:
fmt.Print(string(caps_list[string(cap)]))
No it is not a string. It is a type, wich base type is a string.
So the exact type is important. You can also not add celcius to fahrenheit degrees, even if both types would have integer base types.