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

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.

Try returnBeaconData = append(returnBeaconData, *beacondataXY)

new() built-in function returns a pointer, you can alternatively write:

var beacondataXY = types.BeaconDataXY{}