I'm trying to append an array to another array (or slice to struct?) The result from a query (target) is iterated and appended with result from another query (target per checkpoint) based on previous query result.id
Then the result is iterated in a template.
Model
type Target struct {
ID uint32 `db:"id"`
CriteriaID string `db:"criteria_id"`
TargetCheck []*TargetCheck
}
type TargetCheck struct {
ID uint32 `db:"id"`
TargetID string `db:"target_id"`
CheckpointID template.HTML `db:"checkpoint_id"`
Name string `db:"name"`
}
func TargetByCriteriaID(criteriaID string) ([]Target, error) {
... <return rows>
}
func CheckpointByTargetID(targetID uint32) ([]TargetCheck, error) {
... <return rows>
}
func (target *Target) AddItem(TargetCheckItem *TargetCheck) []*TargetCheck {
target.TargetCheck = append(target.TargetCheck, TargetCheckItem)
return target.TargetCheck
}
Controller
func CriteriaBrowseGET(w http.ResponseWriter, r *http.Request) {
criteriaID := 5
target, err := model.TargetByCriteriaID(criteriaID)
for i := range target {
targetCheck, err := model.CheckpointByTargetID(target[i].ID)
target = model.Target.AddItem(targetCheck)
}
v := view.New(r)
v.Name = "criteria/browse"
v.Vars["target"] = target
v.Render(w)
}
View
{{range $index, $content := .target}}
<p>Target: {{.ID}}</p>
{{if .}}
{{range .TargetCheck}}
<p>{{.CheckpointID}}</p>
<p>{{.Name}}</p>
{{end}}
{{end}}
{{end}}
When performing go build
it says:
vendor\app\controller\criteria.go:74:24: invalid method expression model.Target.AddItem (needs pointer receiver: (*model.Target).AddItem)
vendor\app\controller\criteria.go:74:24: model.Target.AddItem undefined (type model.Target has no method AddItem)
Is there any problem with the code above?