Is it possible to mimic Go interface in C/C++?

Yes. You can use a pure abstract class, and use a template class to wrap types "implementing" the abstract class so that they extend the abstract class. Here's a barebones example:

#include <iostream>

// Interface type used in function signatures.
class Iface {
public:
        virtual int method() const = 0;
};

// Template wrapper for types implementing Iface
template <typename T>
class IfaceT: public Iface {
public:
        explicit IfaceT(T const t):_t(t) {}
        virtual int method() const { return _t.method(); }

private:
        T const _t;
};

// Type implementing Iface
class Impl {
public:
        Impl(int x): _x(x) {}
        int method() const { return _x; }

private:
        int _x;
};


// Method accepting Iface parameter
void printIface(Iface const &i) {
        std::cout << i.method() << std::endl;
}

int main() {
        printIface(IfaceT<Impl>(5));
}

Yes, of course.

In fact the code that handles interfaces in the runtime is written in C. http://code.google.com/p/go/source/browse/src/pkg/runtime/iface.c

Tags:

Go