结构上的GoLang数组索引

时间:2018-07-13 22:01:10

标签: arrays go struct

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

为什么我不能访问数组行?

无效操作:array [i](类型* [] Bar不支持索引)

1 个答案:

答案 0 :(得分:4)

将for循环中的行更改为library(data.table) melt(setDT(x), id=1:2, measure=list(c("t2","t3"), c("y2","y3"), c("se2","se3")), value.name=c("t2", "y2", "se2")) #Or in short form melt(setDT(x), id=1:2, measure=patterns("^t[2-3]", "^y", "^se"), value.name=c("t2", "y2", "se2")) # id t1 variable t2 y2 se2 # 1: Study1 1 1 2 0.1 0.5 # 2: Study1 1 2 3 0.2 0.7

fmt.Println((*array)[i])将尝试取消引用*array[i]

[i]将取消引用(*array)[i],这是您的指针。

工作示例:https://play.golang.org/p/yr6WbtS3Aq_c