Inconsistent nil for pointer receiver (Go error?)

I was making a simple linked list interface to find out about Go interfaces when I came across this apparent inconsistency. nextTalways zero, but the return value is next()not equal.

package main

import (
    "fmt"
)

type LinkedList interface {
    next() LinkedList
}

type T struct {
    nextT *T
}

func (t *T) next() LinkedList {
    //uncomment to see the difference
    /*if t.nextT == nil {
         return nil
    }*/
    return t.nextT//this is nil!
}

func main() {
    t := new(T)
    fmt.Println(t.nextT == nil)

    var ll LinkedList
    ll = t
    fmt.Println(ll.next() == nil)//why isn't this nil?
}

Without checking nil (which I don't need to do) in next()I get

true
false

With this, I get the expected result.

true
true

Did I find a mistake or is this unexpected intention for some reason? Work on Windows with Go version 1 using a zip installation (without MSI)

+5
source share
1 answer

, . Go - : . nil , , , , .

, *T , . nil, . , , , . ( reflect), , . , (, , , nil ). , *T, - nil ( ).

container/list Go, . Russ Cox, Go.

+7

All Articles