How to access the embedded struct inside the Slice of Pointers field of a Struct

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

How to access the embedded struct inside the Slice of Pointers field of a Struct

问题

我想要添加一个功能,当数据是[]*struct时,取第一个元素。

  1. func getEncFields(t reflect.Type, list map[string]int) {
  2. for t.Kind() == reflect.Ptr {
  3. t = t.Elem()
  4. }
  5. if t.Kind() == reflect.Struct {
  6. for i := 0; i < t.NumField(); i++ {
  7. field := t.Field(i)
  8. tag := field.Tag.Get("bson")
  9. if containsTag(tag, "encr") {
  10. list[getFieldName(field, tag)]++
  11. }
  12. getEncFields(field.Type, list)
  13. }
  14. }
  15. }

在这段代码中,当数据是[]*Struct时,我需要添加功能。以下是要传递给此函数的结构类型。

  1. type Customer struct {
  2. Name string `json:"name" bson:"name"`
  3. Acnumber int64 `json:"acnumber" bson:"acnumber,encr"`
  4. Number int64 `json:"number" bson:"number,encr"`
  5. Address []*Address `json:"address" bson:"address"`
  6. }
  7. type Address struct {
  8. Mail string `json:"mail" bson:"mail,encr"`
  9. }

谢谢您的支持。

英文:

I want to add functionality to take the first element when the data is []*struct.

  1. func getEncFields(t reflect.Type, list map[string]int) {
  2. for t.Kind() == reflect.Ptr {
  3. t = t.Elem()
  4. }
  5. if t.Kind() == reflect.Struct {
  6. for i := 0; i &lt; t.NumField(); i++ {
  7. field := t.Field(i)
  8. tag := field.Tag.Get(&quot;bson&quot;)
  9. if containsTag(tag, &quot;encr&quot;) {
  10. list[getFieldName(field, tag)]++
  11. }
  12. getEncFields(field.Type, list)
  13. }
  14. }

In this code I need to add functionality when the data is []*Struct. Here is the struct type to be passed in this function.

  1. type Customer struct {
  2. Name string `json:&quot;name&quot; bson:&quot;name&quot;`
  3. Acnumber int64 `json:&quot;acnumber&quot; bson:&quot;acnumber,encr&quot;`
  4. Number int64 `json:&quot;number&quot; bson:&quot;number,encr&quot;`
  5. Address []*Address `json:&quot;address&quot; bson:&quot;address&quot;`
  6. }
  7. type Address struct {
  8. Mail string `json:&quot;mail&quot; bson:&quot;mail,encr&quot;`
  9. }

Thank you for your support

答案1

得分: 3

通过指针,可以像遍历数组、切片和映射一样深入查看。

  1. func getEncFields(t reflect.Type, list map[string]int) {
  2. for t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice || t.Kind() == reflect.Array || t.Kind() == reflect.Map {
  3. t = t.Elem()
  4. }
  5. if t.Kind() == reflect.Struct {
  6. for i := 0; i < t.NumField(); i++ {
  7. field := t.Field(i)
  8. tag := field.Tag.Get("bson")
  9. if containsTag(tag, "encr") {
  10. list[getFieldName(field, tag)]++
  11. }
  12. getEncFields(field.Type, list)
  13. }
  14. }
  15. }

你可以在这里查看代码示例:https://go.dev/play/p/1msFGC89NX-

英文:

Drill down through arrays, slices and maps as you do for pointers.

  1. func getEncFields(t reflect.Type, list map[string]int) {
  2. for t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice || t.Kind() == reflect.Array || t.Kind() == reflect.Map {
  3. t = t.Elem()
  4. }
  5. if t.Kind() == reflect.Struct {
  6. for i := 0; i &lt; t.NumField(); i++ {
  7. field := t.Field(i)
  8. tag := field.Tag.Get(&quot;bson&quot;)
  9. if containsTag(tag, &quot;encr&quot;) {
  10. list[getFieldName(field, tag)]++
  11. }
  12. getEncFields(field.Type, list)
  13. }
  14. }
  15. }

https://go.dev/play/p/1msFGC89NX-

huangapple
  • 本文由 发表于 2023年5月2日 23:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76156434.html
匿名

发表评论

匿名网友

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

确定