Since I am from Java and new to Golang, I will try to explain what I want in Java.
interface Car { }
class MyCarA implements Car {
int specificToA
}
class MyCarB implements Car {
int specificToB
}
I think such an interface (like Car) is called a marker interface in Java. It is just to indicate the compiler the necessary abstraction.
How can I do this in Golang ?
I have
type MyCarA struct {
specificToA int
}
type MyCarB struct {
specificToB int
}
How can I generalise these structs now ? Should it be an interface or another struct ?
You can do this:
type Car interface { IAmACar() }
type MyCarA struct {
specificToA int
}
func (MyCarA) IAmACar() {}
type MyCarB struct {
specificToB int
}
func (MyCarB) IAmACar() {}
You test for the marker using a type assertion:
_, itIsACar := v.(Car)
The Car interface can also be used to detect errors statically:
var c Car
c = MyCarA{0} // ok
c = 0 // error, int is not a car
The go/ast package does something similar. See uses of the function exprNode in the file ast.go.
The above method is correct & works for runtime detection, but it won't provide compile time detection. If you want compile time detection, pass the type to a function which takes the interface as an argument and check. See sample below:
package main
import "fmt"
type MyType struct {
a int
b int
}
type NotMyType struct {
a int
b int
}
type Printer interface {
Print(a string) error
}
func (m *MyType) Print(s string) error {
fmt.Println(m.a, m.b, s)
return nil
}
//Uncomment following function to see compilation work
// func (m *NotMyType) Print(s string) error {
// fmt.Println(m.a, m.b, s)
// return nil
// }
func main() {
t := &MyType{
a: 1, b: 2,
}
t1 := &NotMyType{
a: 1, b: 2,
}
checkPrintable(t)
checkPrintable(t1)
}
func checkPrintable(p Printer) {
p.Print("Test message")
}
To make it work you need to uncomment the Print function for NotMyType.
Hope this helps.