duan47676379 2017-01-31 14:09
浏览 130
已采纳

Golang切片追加

I am having a problem when appending to my slice using Golang.

Here is my code:

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

The beacondataxy.go contains this:

package types

type BeaconDataXY struct {
    InstanceID  string
    NamespaceID string
    XCoord      float64
    YCoord      float64
    Distance    float64
}

The error message is this:

utils.go:139: cannot use beacondataXY (type *types.BeaconDataXY) as type []types.BeaconDataXY in append

I don't really know how to handle slices in Golang, even after reading some tutorials that makes perfect sense. I'm not sure what I'm doing wrong.

I want to have an array/slice with types inside, return BeaconData is of []types. BeaconDataXY and it should contain single types of BeaconDataXY.

Thanks for all help.

EDIT:

The code now looks like this:

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)
    }
  • 写回答

2条回答 默认 最新

  • dpmrakfbx820320638 2017-01-31 14:24
    关注

    With this assignment:

    var beacondataXY = new(types.BeaconDataXY)
    

    you are creating a variable of type *types.BeaconDataXY. Just create a new BeaconDataXY like this:

    var beacondataXY = types.BeaconDataXY{}
    

    When appending to your array do it like this:

    returnBeaconData = append(returnBeaconData, beacondataXY)
    

    The "..." would assume that beacondataXY is an array but it isn't, you just want to append beacondataXY to returnBeaconData. See https://golang.org/ref/spec#Appending_and_copying_slices for an explanation of what "..." means in this context.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?