如何使用反射(reflect)在Go语言中获取结构体子字段的项目数量

huangapple go评论87阅读模式
英文:

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

问题

通过reflect包从切片结构的子字段中获取项目数量时,我遇到了一些问题。

这是我尝试从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)
  }

  //我想要获取项目的数量,在我的例子中是3
  NumItems := reflect.ValueOf(variable.Stuff[0].Items).Len()

}

这是错误信息:

variable.Items未定义(类型[]Stuff没有字段或方法Items)

我不确定是否可以像那样检索项目的数量。

英文:

I have some issues when getting the number of items from a sub field in a slice struct through reflect package.

This is how I'm trying to get the number of items from 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.

答案1

得分: 0

我已经解决了这个问题。

为了获取子字段的数量,我们可以使用reflect.ValueOf中的Len()函数。

现在的代码正在获取项目的数量:

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

	//我想要获取项目的数量,即3
	t := reflect.ValueOf(variable.Stuff[0].Items)
	fmt.Println(t.Len())

}

输出3
英文:

I have already fixed the issue.

In order to get the number of sub fields we can make use of Len() from reflect.ValueOf.

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

huangapple
  • 本文由 发表于 2022年8月29日 18:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/73527283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定