Go: defining methods on struct types

In Go it’s possible to define methods on struct types. The syntax needed for it can be a bit strange for people that are used to define classes and methods in Java, C# etc… but once you learn it it’s quite easy to use.

In my case for example I needed something that could contain a Timer object, a string and a method that could start the timer and call a method at the end of the Timer execution. I implemented it in this way:

1
2
3
4
5
6
7
8
9
type DeviceTimer struct {
    DeviceID    string
    DeviceTimer *time.Timer
}

func (timer DeviceTimer) startTimer() {
    <-timer.DeviceTimer.C
    notifyDeviceTimerExpired(timer.DeviceID)
}

The key point is row 6 func (timer DeviceTimer) startTimer() { ... } where I defined a method called startTimer and I specify timer DeviceTimer inside the func definition. This basically “extends” the struct DeviceTimer adding that method to it. This means that I can call that method in this way:

1
2
3
timer := time.NewTimer(time.Millisecond * 300)
device_timer := DeviceTimer{"abc123", timer}
go device_timer.startTimer()

This is all you need to do. If you want to read more about this subject, I can suggest to read these two articles:

1
2
3
4
5

**Note:** I'm not a Go expert and these are just my personal notes I'm
taking during my learning experience. I'm very keen to share my notes
with everyone, but please don't take them as notes from an expert Go
developer.
comments powered by Disqus
source code available on GitHub
Built with Hugo
Theme Stack designed by Jimmy