If I have the two modules mod1 and mod2, where mod2 uses mod1 (example source below). The mod1 module has a Results function which just returns a slice of a custom Result struct, which just contains data (a Name). The mod2 module calls this function and returns just the Names.
I understand that calling Result requires a context to generate the list of results, but once that is returned I'm finding I'm a little confused as to why
Result.Nameis function (not a field), and- calling
Result.Namerequires itself a context
Does this mean that this requires executing a query for each result to obtain just the Names? I suppose my expectation was that once the slice is returned the fields would be also available immediately.
mod1/main.go
package main
type Mod1 struct{}
type Result struct {
Name string
}
func (m *Mod1) Results() []Result {
return []Result{
{Name: "bob"},
{Name: "foo"},
{Name: "bar"},
}
}
mod2/main.go
package main
import "context"
type Mod2 struct{}
func (m *Mod2) Names(ctx context.Context) ([]string, error) {
results, err := dag.Mod1().Results(ctx)
if err != nil {
return nil, err
}
names := make([]string, len(results))
for i, r := range results {
name, err := r.Name(ctx) // Why does this need a context?
if err != nil {
return nil, err
}
names[i] = name
}
return names, nil
}