How does a type become a function in Go?

I am new to Go.

I am looking at some Kubernetes source code .

I see it:

// GetByKey returns the key if it exists in the list returned by kl.
func (kl keyLookupFunc) GetByKey(key string) (interface{}, bool, error) {
    for _, v := range kl() {
        if v.name == key {
            return v, true, nil
        }
    }
    return nil, false, nil
}

I know vaguely how to read this, but I’m sure that I will be mistaken in my terminology: it is called somewhere keyLookupFunc, but klis its copy, and this function, called GetByKey. It takes keywhose type string, and it returns three values, etc. Etc.

(I don’t see the BNF for this particular construct, my best guess is where it should live in the language specification , but I saw this construct several times before I take it on faith.)

In the source code above, I noticed this:

// keyLookupFunc adapts a raw function to be a KeyLookup.
type keyLookupFunc func() []testFifoObject

, keyLookupFunc , , , testFifoObject s.

, keyLookupFunc -typed , GetByKey "on". , , .

, , , :

func TestDeltaFIFO_requeueOnPop(t *testing.T) {
    f := NewDeltaFIFO(testFifoObjectKeyFunc, nil, nil)

    f.Add(mkFifoObj("foo", 10))
    _, err := f.Pop(func(obj interface{}) error {
        if obj.(Deltas)[0].Object.(testFifoObject).name != "foo" {
            t.Fatalf("unexpected object: %#v", obj)
        }
        return ErrRequeue{Err: nil}
    })
    if err != nil {
        t.Fatalf("unexpected error: %v", err)
    }
    if _, ok, err := f.GetByKey("foo"); !ok || err != nil {
        t.Fatalf("object should have been requeued: %t %v", ok, err)
    }

f.GetByKey("foo"). f DeltaFIFO, , NewDeltaFIFO.

, f DeltaFIFO, keyLookupFunc , GetByKey "on" it? ?

+4
2

, GetByKey ( ):

, f.GetByKey:

// GetByKey returns the complete list of deltas for the requested item,
// setting exists=false if that list is empty.
// You should treat the items returned inside the deltas as immutable.
func (f *DeltaFIFO) GetByKey(key string) (item interface{}, exists bool, err error) {
    f.lock.RLock()
    defer f.lock.RUnlock()
    d, exists := f.items[key]
    if exists {
        // Copy item slice so operations on this slice (delta
        // compression) won't interfere with the object we return.
        d = copyDeltas(d)
    }
    return d, exists, nil
}

( delta_fifo.go return f.GetByKey(key)):

// Get returns the complete list of deltas for the requested item,
// or sets exists=false.
// You should treat the items returned inside the deltas as immutable.
func (f *DeltaFIFO) Get(obj interface{}) (item interface{}, exists bool, err error) {
    key, err := f.KeyOf(obj)
    if err != nil {
        return nil, false, KeyError{obj, err}
    }
    return f.GetByKey(key)
}

( delta_fifo_test.go f.GetByKey("foo")):

func TestDeltaFIFO_requeueOnPop(t *testing.T) {
    f := NewDeltaFIFO(testFifoObjectKeyFunc, nil, nil)

    f.Add(mkFifoObj("foo", 10))
    _, err := f.Pop(func(obj interface{}) error {
        if obj.(Deltas)[0].Object.(testFifoObject).name != "foo" {
            t.Fatalf("unexpected object: %#v", obj)
        }
        return ErrRequeue{Err: nil}
    })
    if err != nil {
        t.Fatalf("unexpected error: %v", err)
    }
    if _, ok, err := f.GetByKey("foo"); !ok || err != nil {
        t.Fatalf("object should have been requeued: %t %v", ok, err)
    }

    _, err = f.Pop(func(obj interface{}) error {
        if obj.(Deltas)[0].Object.(testFifoObject).name != "foo" {
            t.Fatalf("unexpected object: %#v", obj)
        }
        return ErrRequeue{Err: fmt.Errorf("test error")}
    })
    if err == nil || err.Error() != "test error" {
        t.Fatalf("unexpected error: %v", err)
    }
    if _, ok, err := f.GetByKey("foo"); !ok || err != nil {
        t.Fatalf("object should have been requeued: %t %v", ok, err)
    }

    _, err = f.Pop(func(obj interface{}) error {
        if obj.(Deltas)[0].Object.(testFifoObject).name != "foo" {
            t.Fatalf("unexpected object: %#v", obj)
        }
        return nil
    })
    if err != nil {
        t.Fatalf("unexpected error: %v", err)
    }
    if _, ok, err := f.GetByKey("foo"); ok || err != nil {
        t.Fatalf("object should have been removed: %t %v", ok, err)
    }
}

:

// GetByKey returns the key if it exists in the list returned by kl.
func (kl keyLookupFunc) GetByKey(key string) (interface{}, bool, error) {
    for _, v := range kl() {
        if v.name == key {
            return v, true, nil
        }
    }
    return nil, false, nil
}

( ).

+4

Source: https://habr.com/ru/post/1689130/


All Articles