好久没写go语言,语法快忘记了

main.go

package main

import (
    "fmt"
)

type Comparable interface {
    CompareTo(c Comparable) int
}

type Integer struct {
    val int
}
func (t Integer) intValue() int {
    return t.val
}
func (t Integer) CompareTo(c Comparable) int {
    var i interface{} = c.(Integer)
    var num = i.(Integer)
    return t.intValue() - num.intValue()
}

func Max(a Comparable, b Comparable) Comparable {
    if a.CompareTo(b) > 0 {
        return a
    }
    return b
}

func IntegerValueOf(i int) Integer {
    return Integer{val: i}
}

func main() {
    x := 3
    y := 4
    z := 5
    maxXy := Max(IntegerValueOf(x), IntegerValueOf(y))
    i := maxXy.(Integer)
    fmt.Println(i.intValue()) // 4

    maxXz := Max(IntegerValueOf(x), IntegerValueOf(z))
    i = maxXz.(Integer)
    fmt.Println(i.intValue())  // 5
}

interface{} 用法如上所示