douzheng9221 2018-07-20 07:00
浏览 54
已采纳

将切片附加到Golang中的结构

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?

  • 写回答

2条回答 默认 最新

  • dtj88302 2018-07-20 09:07
    关注

    The error clearly indicates:

    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)

    that you have pointer receiver to method AddItem here:

    func (target *Target) AddItem(TargetCheckItem *TargetCheck) []*TargetCheck {
        target.TargetCheck = append(target.TargetCheck, TargetCheckItem)
        return target.TargetCheck
    }
    

    But when you are calling the function from controller your are using value receiver:

        targetCheck, err := model.CheckpointByTargetID(target[i].ID)
        target = model.Target.AddItem(targetCheck)
    

    You should pass pointer receiver when calling the function AddItem.

    Playground Example

    It is described in Golang spec for Method Declaration:

    The receiver is specified via an extra parameter section preceding the method name. That parameter section must declare a single non-variadic parameter, the receiver. Its type must be of the form T or *T (possibly using parentheses) where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be defined in the same package as the method. The method is said to be bound to the base type and the method name is visible only within selectors for type T or *T.

    Given type Point, the declarations:

    func (p *Point) Length() float64 { return math.Sqrt(p.x * p.x + p.y * p.y) }

    func (p *Point) Scale(factor float64) {
        p.x *= factor
        p.y *= factor
    }
    

    bind the methods Length and Scale, with receiver type *Point, to the base type Point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源