I have seen a fair amount of blogs & videos on Go and as far as I recall, none of the authors use \'self\' or \'this\' for the receiver variable when writing methods. H
From https://blog.heroku.com/neither-self-nor-this-receivers-in-go.
func (this *Room) Announce() {
srv := this.Server()
for _, c := range srv.Clients() {
// Send announcement to all clients about a new room
c.Send(srv.RenderAnnouncement(this))
}
}
// Moved between...
func (this *Server) AddRoom(room *Room) {
for _, c := range this.Clients() {
// Send announcement to all clients about a new room
c.Send(this.RenderAnnouncement(room))
}
}
When using this, there is confusion about whether we're referring to the server or the room as we're moving the code between.
- c.Send(this.RenderAnnouncement(room))
+ c.Send(srv.RenderAnnouncement(this))
Refactoring this kind of code produce some bugs that the compiler will hopefully catch (or maybe not, if the interfaces happen to be compatible). Even bugs aside, having to edit all the little innards does make moving code around more tedious.
Moving across levels of abstraction is a great example of when consistently well-named receivers make a huge difference:
func (room *Room) Announce() {
srv := room.Server()
for _, c := range srv.Clients() {
// Send announcement to all clients about a new room
c.Send(srv.RenderAnnouncement(room))
}
}
// Moved between...
func (srv *Server) AddRoom(room *Room) {
for _, c := range srv.Clients() {
// Send announcement to all clients about a new room
c.Send(srv.RenderAnnouncement(room))
}
}