Skip to the content.

Go Interface 示例

hello interface

type error interface {
    Error() string
}

type MyError struct {
    When time.Time
    What string
}

func (e *MyError) Error() string {
    return fmt.Sprintf("at %v, %s",
        e.When, e.What)
}

func TestInterfaceError(t *testing.T) {

    var err error
    err = &MyError{When: time.Now(), What: "something's wrong"}

    if err != nil {
        // at 2022-11-24 09:43:24.774546757 +0800 CST m=+0.000640449, something's wrong
        fmt.Println(err)
    }

}

断言

func TestAssert(t *testing.T) {
    var itf interface{}

    itf = 1

	// i 1
    fmt.Println("i", itf.(int))

    // panic: interface conversion: interface {} is int, not int64
    // fmt.Println("i64", itf.(int64))

    s, ok := itf.(string)
    // s  true false
    fmt.Println("s", s, s == "", ok)

    // int
    switch itf.(type) {
    case int:
        fmt.Println("int")
    case string:
        fmt.Println("string")
    }
}

nil

func TestInterfaceNil(t *testing.T) {

	type helloInterface interface {
		Hello()
	}

	var i helloInterface
	// true
	fmt.Println("i == nil", i == nil)

	var u *User
	i = u

	// false
	fmt.Println("i == nil", i == nil)
	// true
	fmt.Println("u == nil", u == nil)
}

interface 继承

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

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

type ReadWriter interface {
    Reader
    Writer
}

参考