简体   繁体   English

如何使用 go 中的反射从结构子字段中获取项目数

[英]How to get the number of items from a structure sub field using reflect in go

I have some issues when getting the number of items from a sub field in a slice struct through reflect package.通过reflect package 从切片结构中的子字段获取项目数时遇到一些问题。

This is how I'm trying to get the number of items from Items这就是我试图从Items中获取项目数量的方式

func main() {

  type Items struct {
      Name    string `json:"name"`
      Present bool   `json:"present"`
  }

  type someStuff struct {
      Fields string     `json:"fields"`
      Items    []Items `json:"items"`
  }

  type Stuff struct {
      Stuff []someStuff `json:"stuff"`
  }

  some_stuff := `{
                  "stuff": [
                     {
                       "fields": "example",
                       "items": [
                         { "name": "book01", "present": true },
                         { "name": "book02", "present": true },
                         { "name": "book03", "present": true }                                        
                       ]
                     }
                   ]
                }`

  var variable Stuff

  err := json.Unmarshal([]byte(some_stuff), &variable)
  if err != nil {
      panic(err)
  }

  //I want to get the number of items in my case 3
  NumItems := reflect.ValueOf(variable.Stuff.Items)

}

This is the error:这是错误:

variable.Items undefined (type []Stuff has no field or method Items)

I'm unsure if I can retrieve the number of items like that.我不确定我是否可以检索到这样的项目数量。

I have already fixed the issue.我已经解决了这个问题。

In order to get the number of sub fields we can make use of Len() from reflect.ValueOf .为了获得子字段的数量,我们可以使用reflect.ValueOf中的Len()

The code now is getting the number of Items:现在的代码正在获取项目的数量:

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

func main() {

    type Items struct {
        Name    string `json:"name"`
        Present bool   `json:"present"`
    }

    type someStuff struct {
        Fields string  `json:"fields"`
        Items  []Items `json:"items"`
    }

    type Stuff struct {
        Stuff []someStuff `json:"stuff"`
    }

    some_stuff := `{
                  "stuff": [
                     {
                       "fields": "example",
                       "items": [
                         { "name": "book01", "present": true },
                         { "name": "book02", "present": true },
                         { "name": "book03", "present": true }                                        
                       ]
                     }
                   ]
                }`

    var variable Stuff

    err := json.Unmarshal([]byte(some_stuff), &variable)
    if err != nil {
        panic(err)
    }

    //I want to get the number of items in my case 3
    t := reflect.ValueOf(variable.Stuff[0].Items)
    fmt.Println(t.Len())

}

Output: 3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Go 获得 XML 中指定的结构? - How to get structure specified in XML with Go? 使用where子句过滤集合字段后从子集合中获取数据并提供给stream builder - Get the data from sub collection after filtering the field of collection using where clause and provide it to stream builder 如何在不使用 get() 的情况下根据父集合中的字段实现对 firestore 子集合的控制? - How to achieve control over a firestore sub-collection depending on a field in a parent collection without using get()? GO:我如何使用 go 反射来设置结构指针的值 - GO: how can i use go reflect to set the value of pointer of struct 如何从子集合中获取数据 - How to get data from sub collection 如何使用 AWS Go SDK 仅更新 DynamoDB 中的单个字段 - How to only update a single field in DynamoDB using AWS Go SDK 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? 修改我正在使用的 package 的底层 Go 子依赖 - Modify underlying Go sub dependency on the package I am using 如何使用 BigQuery 从另一个表中获取包含相同项目的交易? - How do I get transactions that contains the same items from another table using BigQuery? 如何在 go redis 客户端中使用 igbinary 序列化 GET 和 SET - How to serialize GET and SET using igbinary inside go redis client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM