在Golang中,我应该将哪种类型用于仅方法类型?

时间:2022-12-16 10:24:41

I mean a method-only type like this

我的意思是像这样的方法类型

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) {}

I see this in binary.BigEndian

我在二进制文件中看到了这一点.BigEndian

// 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{}

This is a very tricky way to group your method. So the question is: why struct{}? Why not just a int alias, is there any reason to choose struct{} over other types?

这是对方法进行分组的一种非常棘手的方法。所以问题是:为什么struct {}?为什么不只是一个int别名,有没有理由选择struct {}而不是其他类型?

1 个解决方案

#1


I believe that the main difference between struct {} and int is that struct {} takes up no memory whereas an unused int would waste 4 or 8 bytes.

我相信struct {}和int之间的主要区别在于struct {}不占用内存,而未使用的int会浪费4或8个字节。

#1


I believe that the main difference between struct {} and int is that struct {} takes up no memory whereas an unused int would waste 4 or 8 bytes.

我相信struct {}和int之间的主要区别在于struct {}不占用内存,而未使用的int会浪费4或8个字节。