Equivalence of abstract classes/methods (Java) in Google Go

You can have composite interfaces, for example from the io package :

http://golang.org/src/pkg/io/io.go?s=2987:3047#L57

type Reader interface {
    Read(p []byte) (n int, err error)
}
type Writer interface {
    Write(p []byte) (n int, err error)
}

type ReadWriter interface {
    Reader
    Writer
}

As a side note, don't try to implement java code using go, try to learn the Go Way.


Since Go does not have static methods in the OOP sense, you often see those types of methods being implemented as package level functions:

package mypackage

func() Method1() { ... } // Below I will call it Function instead

Such package level functions would then take an interface as an argument. Your code would in that case look something like this:

package main

import "fmt"

type Methoder interface {
    Method()
}

func Function(m Methoder) {
    m.Method()
}

type StructB struct{}

func (s *StructB) Method() { fmt.Println("StructB") }

type StructC struct{} // You can do some "inheritance" by embedding a base struct

func (s *StructC) Method() { fmt.Println("StructC") }

func main() {    
    b := &StructB{}
    Function(b)    
}

Output:

StructB