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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵