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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能