4
package main

import "fmt"

type Bar struct {
    high float64
    low  float64
}

func main() {
    var bars = []Bar{}
    bars = []Bar{
        {1.0, 2.0},
        {1.1, 2.1},
        {1.2, 2.2},
        {1.3, 2.3},
        {1.4, 2.4},
        {1.5, 2.5},
    }
    fmt.Println(bars)
    testFunction(&bars)
}

func testFunction(array *[]Bar) {
    for i := 0; i < 3; i++ {
        fmt.Println(*array[i])
    }
}

https://play.golang.org/p/MZwaFALHfuy

Why can I not access the array row?

invalid operation: array[i] (type *[]Bar does not support indexing)

1 Answer 1

7

Change the line in for loop to fmt.Println((*array)[i])

*array[i] would try to dereference [i]

(*array)[i] would derefence the array which is your pointer.

Working example: https://play.golang.org/p/yr6WbtS3Aq_c

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

3 Comments

Array in function is not the row index. How would you get access to the second element in the row since you can not do [i][]i] ?
fmt.Println((*array)[i].low) . Those are Bar structs, with values for high and low. While you initialize them in what looks like an array or a slice, they aren't arrays, they are of type Bar.
I was looking at the array like #7 on this post stackoverflow.com/questions/5868927/…

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.