Oop 在Golang中,我应该将哪种类型用于仅方法类型?
我的意思是像这样的方法类型Oop 在Golang中,我应该将哪种类型用于仅方法类型?,oop,go,Oop,Go,我的意思是像这样的方法类型 var Util util type util struct { } func (util)Help(v VM) {} func (util)HelpMe(v VM) {} func (util)HelpYou(v VM) {} func (util)HelpEveryOne(v VM) {} 我看到了这一点 这是对方法进行分组的一种非常棘手的方法。所以问题是:为什么struct{}?为什么不仅仅是一个int别名,有没有理由选择struct{}而不是其他类型?我认为
var Util util
type util struct { }
func (util)Help(v VM) {}
func (util)HelpMe(v VM) {}
func (util)HelpYou(v VM) {}
func (util)HelpEveryOne(v VM) {}
我看到了这一点
这是对方法进行分组的一种非常棘手的方法。所以问题是:为什么
struct{}
?为什么不仅仅是一个int
别名,有没有理由选择struct{}
而不是其他类型?我认为struct{}
和int
之间的主要区别在于struct{}
不占用内存,而未使用的int
将浪费4或8个字节。我认为struct{}
和int
之间的主要区别在于struct{}
不占用内存,而未使用的int
将浪费4或8个字节
// LittleEndian is the little-endian implementation of ByteOrder.
var LittleEndian littleEndian
// BigEndian is the big-endian implementation of ByteOrder.
var BigEndian bigEndian
type littleEndian struct{}