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条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)