user6256186 user6256186 - 1 year ago 146
C++ Question

Elegant way to implement template method pattern in Golang

Is there an elegant canonical way to implement template method pattern in Go?
In C++ this looks like this:

#include <iostream>
#include <memory>

class Runner {
void Start() {
// some prepare stuff...
virtual void Run() = 0;

class Logger : public Runner {
virtual void Run() override {
std::cout << "Running..." << std::endl;

int main() {
std::unique_ptr<Runner> l = std::make_unique<Logger>();
return 0;

In golang i wrote something like this:

package main

import (

type Runner struct {
doRun func()
needStop bool

func (r *Runner) Start() {
go r.doRun()

func NewRunner(f func()) *Runner {
return &Runner{f, false}

type Logger struct {
i int

func NewLogger() *Logger {
l := &Logger{}
l.doRun = l.doRunImpl
return l

func (l *Logger) doRunImpl() {
time.Sleep(1 * time.Second)

func main() {
l := NewLogger()
fmt.Println("Hello, playground")

But this code fails with runtime null pointer error.
Basic idea is to mix in some functionality from derived classes (go structs) to the base class routine in a way that base class state is available from this mix-in derived routine.

Answer Source

Logger embeds a pointer which will be nil when you allocate the struct. That's because embedding does not put everything inside the struct, it actually creates a field (named Runner of type *Runner in your case) and the language gives you some syntactic sugar to access what's inside it. In your case it means that you can access Runner fields in two ways:

l := Logger{}
l.needStop = false
l.Runner.needStop = false

To fix the error you need to allocate Runner field inside the Logger like so:

l := Logger{Runner:&Runner{}}

Or embed by value instead of pointer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download