wener
wener

Reputation: 7740

In Golang which type should I use for a method-only type?

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

// 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?

Upvotes: 1

Views: 82

Answers (1)

jochen
jochen

Reputation: 3920

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.

Upvotes: 5

Related Questions