package main
import "fmt"
type Number int
func (n *Number) IncreaseMe(i int) {
*n += i
}
func main() {
n := Number(10)
n.IncreaseMe(90) // n is now supposed to be 100
fmt.Println(n)
}
When running the code above, it gives me the error message
invalid operation: *n += i (mismatched types Number and int)
Which is to be expected as it's trying to do a math operation on variables which don't share the same type.
I then tried
*n.(int) += i
which tells the compiler not to worry as *n can be safely treated as an integer, which leads me to
invalid type assertion: n.(int) (non-interface type *Number on left)
I believe this is happening because type assertions works only with interfaces, not custom types.
So what is the solution for this?