Unfortunately not. func main() { c := color.Red cptr := (*string)(&c) *cptr = "orange" PrintColor(c) // successfully compiles, and prints "orange" }
It's the game of the mouse and the cat :-D We can change the package color like this : package color type Color interface { void() } type color struct { v string } func (c color) void() {} func (c color) String() string { return c.v } var ( Red color = color{"red"} Green color = color{"green"} Blue color = color{"blue"} )
func fn(c color.Color) {
switch c {
case color.Red, color.Green, color.Blue:
log.Printf("c=%#+v -- OK", c)
default:
log.Printf("c=%#+v -- should not be possible", c)
}
}
func main() {
var c color.Color
fn(c)
}
Output: c= -- should not be possible
:shrug: