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

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

报告相同问题?

悬赏问题

  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?