从结构迭代中排除空字段。

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

Exclude empty fields from struct iteration

问题

我有一个结构体,它将从用户输入中获取其值。
现在我想提取只有关联值的字段名称。值为nil的字段不应返回。我该如何做到这一点?

以下是我的代码:

  1. package main
  2. import "fmt"
  3. import "reflect"
  4. type Users struct {
  5. Name string
  6. Password string
  7. }
  8. func main() {
  9. u := Users{"Robert", ""}
  10. val := reflect.ValueOf(u)
  11. for i := 0; i < val.NumField(); i++ {
  12. fmt.Println(val.Type().Field(i).Name)
  13. }
  14. }

当前结果:

  1. Name
  2. Password

期望结果:

  1. Name
英文:

I have a struct that will get its value from user input.
Now I want to extract only field names that have associated values. Fields with a nil value should not be returned. How can I do that?

Here’s my code:

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;reflect&quot;
  4. type Users struct {
  5. Name string
  6. Password string
  7. }
  8. func main(){
  9. u := Users{&quot;Robert&quot;, &quot;&quot;}
  10. val := reflect.ValueOf(u)
  11. for i := 0; i &lt; val.NumField(); i++ {
  12. fmt.Println(val.Type().Field(i).Name)
  13. }
  14. }

Current Result:

  1. Name
  2. Password

Expected result:

  1. Name

答案1

得分: 5

你需要编写一个函数来检查是否为空:

  1. func empty(v reflect.Value) bool {
  2. switch v.Kind() {
  3. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  4. return v.Int() == 0
  5. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  6. return v.Uint() == 0
  7. case reflect.String:
  8. return v.String() == ""
  9. case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Interface, reflect.Chan:
  10. return v.IsNil()
  11. case reflect.Bool:
  12. return !v.Bool()
  13. }
  14. return false
  15. }

playground示例

英文:

You need to write a function to check for empty:

  1. func empty(v reflect.Value) bool {
  2. switch v.Kind() {
  3. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  4. return v.Int() == 0
  5. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  6. return v.Uint() == 0
  7. case reflect.String:
  8. return v.String() == &quot;&quot;
  9. case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Interface, reflect.Chan:
  10. return v.IsNil()
  11. case reflect.Bool:
  12. return !v.Bool()
  13. }
  14. return false
  15. }

playground example.

答案2

得分: 0

我认为我找到了一个解决方案。案件结案。:)

  1. import "fmt"
  2. import "reflect"
  3. type Users struct {
  4. Name string
  5. Password string
  6. }
  7. func main(){
  8. u := Users{"robert", ""}
  9. val := reflect.ValueOf(u)
  10. var fFields []string
  11. for i := 0; i < val.NumField(); i++ {
  12. f := val.Field(i)
  13. if f.Interface() != "" {
  14. fFields = append(fFields, val.Type().Field(i).Name)
  15. }
  16. }
  17. fmt.Println(fFields)
  18. }

http://play.golang.org/p/QVIJaNXGQB

英文:

I think I found a solution. Case closed. 从结构迭代中排除空字段。

  1. import &quot;fmt&quot;
  2. import &quot;reflect&quot;
  3. type Users struct {
  4. Name string
  5. Password string
  6. }
  7. func main(){
  8. u := Users{&quot;robert&quot;, &quot;&quot;}
  9. val := reflect.ValueOf(u)
  10. var fFields []string
  11. for i := 0; i &lt; val.NumField(); i++ {
  12. f := val.Field(i)
  13. if f.Interface() != &quot;&quot; {
  14. fFields = append(fFields, val.Type().Field(i).Name)
  15. }
  16. }
  17. fmt.Println(fFields)
  18. }

http://play.golang.org/p/QVIJaNXGQB

huangapple
  • 本文由 发表于 2014年12月1日 13:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/27222018.html
匿名

发表评论

匿名网友

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

确定