1

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Have updated according to that but still same problem as I answered in the other solution. Index out of range
Please post the complete stack trace.
How do I see stack trace? Or is the stack trace the entire error output? imgur.com/wMj90Fz
See previous comment here, posted a imgur link with entire stack trace
What's in line 136 of handlers/utils.go?
|
1

Try returnBeaconData = append(returnBeaconData, *beacondataXY)

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

var beacondataXY = types.BeaconDataXY{}

11 Comments

http: panic serving 127.0.0.1:56095: runtime error: index out of range goroutine 7 [running]:
returnBeaconData is a slice. index out of range happens to arrays.
I've had this problem and I think I did what I did to get rid of the index out of bound error. Although it doesnt work
But what has happened then? I get the error index out of range when changing what you said?
@SimonNilssonGuldstrand 1389 is the line number
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.