Good post. Shows very well which aspects of Go is easy to pick up, and which isn't.
The code examples naturally shows an how a newcomer would approach this.
I have no idea where the author picked up "A major recurring point is that Go seems to advocate runtime over compile time checking", which couldn't be more false IMO.
For fun, I did a quick re-write of how it typically would look:
type Drawer interface {
DrawTo(image.Image)
}
type Line struct {
color Color
a Point
b Point
}
func (d Line) DrawTo(bg image.Image) {}
type Triangle struct {
color Color
fill Color
a Point
b Point
c Point
}
func (d Triangle) DrawTo(bg image.Image) {}
type Fill struct {
color Color
}
func (d Fill) DrawTo(bg image.Image) {}
func paintingLoop(img image.Image, ch chan Drawer) {
// shorthand for a loop over receiving over
// a channel
for msg := range ch {
msg.DrawTo(img)
}
}
func drawThingy(ch chan Drawer) {
ch <- Line {color: Red, a: Point{x:0, y: 0]}, b: Point{x: 1, y: 1}}
ch <- Background{color: Blue}
}
func main() {
// do some setup
ch := make(chan Drawer, 100)
// Create out destination image
var img = image.NewNRGBA(image.Rectangle(100,100))
// Creates a goroutine which will handle the
// event loop
go func() {
// create the window, etc
paintingLoop(ch, img)
}()
drawThingy(ch)
// Close when done.
close(ch)
// TODO: Add a channel to indicate image is done
}