If I define a type definition
type X int64
Why is it that I can then do
var x X = 123
x = x + 1
The x behaves as if it was the underlying int64, which I don't want. Or that it allows operations on the underlying type to be performed on this new type?
One of the reasons I'd define a new type is to hide the underlying type and define my own operations on it.
Creating a new defined type will dissociate any methods on the underlying type, but it does not dissociate functionality with operators such as
+ - / *.You should base your type on an underlying type with the desirable operators. For example, if you don't want to have arithmetic operators, you can derive from a
struct.If your type still needs the arithmetic capabilities of an
int64for internal reasons, you can hide it as an un-exported field in the struct. For example: