I am new to Go.
I am looking at some Kubernetes source code .
I see it:
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 kl
is its copy, and this function, called GetByKey
. It takes key
whose 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:
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? ?