dongxue7306 2016-12-31 03:35
浏览 40
已采纳

如何通过指针访问结构数组?

Following are my 2 structs

type Attempt struct {
    StartTime        string `json:"startTime"`
    EndTime          string `json:"endTime"`
    LastUpdated      string `json:"lastUpdated"`
    Duration         uint32 `json:"duration"`
    SparkUser        string `json:"sparkUser"`
    IsCompleted      bool   `json:"completed"`
    LastUpdatedEpoch int64  `json:"lastUpdatedEpoch"`
    StartTimeEpoch   int64  `json:"startTimeEpoch"`
    EndTimeEpoch     int64  `json:"EndTimeEpoch"`
}

type Apps struct {
    Id       string    `json:"id"`
    Name     string    `json:"name"`
    Attempts []Attempt `json:"attempts"`
}

The following test parses a json string into this apps := &[]Apps{}. When accessing the members of apps, I am getting the following error

invalid operation: apps[0] (type *[]Apps does not support indexing)

The test

func TestUnmarshalApps(t *testing.T) {
    appsJson := `[
      {
        "id": "app-20161229224238-0001",
        "name": "Spark shell",
        "attempts": [
          {
        "startTime": "2016-12-30T03:42:26.828GMT",
        "endTime": "2016-12-30T03:50:05.696GMT",
        "lastUpdated": "2016-12-30T03:50:05.719GMT",
        "duration": 458868,
        "sparkUser": "esha",
        "completed": true,
        "endTimeEpoch": 1483069805696,
        "lastUpdatedEpoch": 1483069805719,
        "startTimeEpoch": 1483069346828
          },
          {
        "startTime": "2016-12-30T03:42:26.828GMT",
        "endTime": "2016-12-30T03:50:05.696GMT",
        "lastUpdated": "2016-12-30T03:50:05.719GMT",
        "duration": 458868,
        "sparkUser": "esha",
        "completed": true,
        "endTimeEpoch": 1483069805696,
        "lastUpdatedEpoch": 1483069805719,
        "startTimeEpoch": 1483069346828
          }
        ]
      },
      {
        "id": "app-20161229222707-0000",
        "name": "Spark shell",
        "attempts": [
          {
        "startTime": "2016-12-30T03:26:50.679GMT",
        "endTime": "2016-12-30T03:38:35.882GMT",
        "lastUpdated": "2016-12-30T03:38:36.013GMT",
        "duration": 705203,
        "sparkUser": "esha",
        "completed": true,
        "endTimeEpoch": 1483069115882,
        "lastUpdatedEpoch": 1483069116013,
        "startTimeEpoch": 1483068410679
          }
        ]
      }
    ]`
    apps := &[]Apps{}
    err := json.Unmarshal([]byte(appsJson), apps)
    if err != nil {
        t.Fatal(err)
    }
    if len(*apps) != 2 {
        t.Fail()
    }

    if len(apps[0].Attempts) != 2 {
        t.Fail()
    }
}

How to access the fields Attempts, Id etc.?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-12-31 03:41
    关注
    apps := &[]Apps{}
    

    apps has type *[]Apps (pointer to slice of Apps objects).

    Are you sure you didn't mean to use the type []*Apps (slice of pointers to Apps objects)?

    Assuming *[]Apps really is the type you intended, you'd need to use (*apps)[i] to access every element of apps. That type is also the reason why you also need to use len(*apps) instead of len(apps) (and *apps for pretty much everything actually).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器