Golang slices append
我在使用golang添加到切片时遇到问题。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | func MatchBeaconWithXY(w http.ResponseWriter, r *http.Request) ([]types.BeaconDataXY, error) { context := appengine.NewContext(r) returnBeaconData := []types.BeaconDataXY{} beacondata, err := GetBeaconData(w, r) if err != nil { log.Errorf(context,"error getting beacondata %v", err) w.WriteHeader(http.StatusInternalServerError) return nil, err } for index, element := range beacondata { q := datastore.NewQuery("physicalbeacondata").Filter("NamespaceID =", element.NamespaceID).Filter("InstanceID =", element.InstanceID) beacondatastatic := []types.BeaconDataStatic{} _, err := q.GetAll(context, &beacondatastatic) if err != nil { log.Errorf(context,"cant get query %v", err) w.WriteHeader(http.StatusInternalServerError) return nil, err } var beacondataXY = new(types.BeaconDataXY) beacondataXY.NamespaceID = element.NamespaceID beacondataXY.InstanceID = element.InstanceID beacondataXY.XCoord = beacondatastatic[0].XCoord beacondataXY.YCoord = beacondatastatic[0].YCoord beacondataXY.Distance = element.Distance returnBeaconData = append(returnBeaconData, beacondataXY...) log.Infof(context,"beaondataXY tot %v", beacondataXY) } |
beacondataxy.go包含以下内容:
1 2 3 4 5 6 7 8 9 | package types type BeaconDataXY struct { InstanceID string NamespaceID string XCoord float64 YCoord float64 Distance float64 } |
号
错误信息如下:
utils.go:139: cannot use beacondataXY (type *types.BeaconDataXY) as
type []types.BeaconDataXY in append
号
我真的不知道如何处理戈兰的切片,即使在阅读了一些非常有意义的教程之后。我不知道我做错了什么。
我想要一个数组/片,里面有类型,返回的beacondata是[]类型。beacondataxy,它应该包含单一类型的beacondataxy。
谢谢你的帮助。
编辑:
代码现在看起来如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | func MatchBeaconWithXY(w http.ResponseWriter, r *http.Request) ([]types.BeaconDataXY, error) { context := appengine.NewContext(r) //returnBeaconData := []types.BeaconDataXY{} returnBeaconData := make([]types.BeaconDataXY, 1) beacondata, err := GetBeaconData(w, r) if err != nil { log.Errorf(context,"error getting beacondata %v", err) w.WriteHeader(http.StatusInternalServerError) return nil, err } for _, element := range beacondata { q := datastore.NewQuery("physicalbeacondata").Filter("NamespaceID =", element.NamespaceID).Filter("InstanceID =", element.InstanceID) beacondatastatic := []types.BeaconDataStatic{} _, err := q.GetAll(context, &beacondatastatic) if err != nil { log.Errorf(context,"cant get query %v", err) w.WriteHeader(http.StatusInternalServerError) return nil, err } var beacondataXY = types.BeaconDataXY{} beacondataXY.NamespaceID = element.NamespaceID beacondataXY.InstanceID = element.InstanceID beacondataXY.XCoord = beacondatastatic[0].XCoord beacondataXY.YCoord = beacondatastatic[0].YCoord beacondataXY.Distance = element.Distance returnBeaconData = append(returnBeaconData, beacondataXY) //log.Infof(context,"beaondataXY tot %v", beacondataXY) } |
有了这个任务:
1 | var beacondataXY = new(types.BeaconDataXY) |
您正在创建类型为*types.becondataxy的变量。只需创建一个新的beacondataxy,如下所示:
1 | var beacondataXY = types.BeaconDataXY{} |
号
当附加到数组时,请执行以下操作:
1 | returnBeaconData = append(returnBeaconData, beacondataXY) |
"…"将假定beacondataxy是一个数组,但它不是,您只需要将beacondataxy附加到returnboacondata。请参阅https://golang.org/ref/spec_appending_and_copying_slices,了解在本文中"…"的含义。
试试
new()内置函数返回指针,您也可以编写:
江户十一〔一〕号