测试golang嵌套结构中的零值 [英] Test for nil values in golang nested stucts

查看:151
本文介绍了测试golang嵌套结构中的零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个深入嵌套的结构。这些由json unmarshaller构建。



然而,在这个结构中有一些字段是'omitifempty',所以我结束了一个在各个地方可能有nills结构的操作。



示例(真实的东西是更深的嵌套,而big:400行的结构):

  package主要

导入fmt

类型Foo结构{
Foo字符串
Bar * Bar
}

类型Bar结构{
Bar字符串
Baz * Baz
}

类型Baz结构{
Baz字符串
}

func main(){
f1:= Foo {Foo:f1}
f2:= Foo {Foo:f2,Bar:& Bar {Bar:br2 }}
f3:= Foo {Foo:f3,Bar:&Bar {Bar:br3,Baz:& Baz {Baz:bz3}}}

fmt.Println(f3.Bar.Baz.Baz)// - > bz3
fmt.Println(f2.Bar.Baz.Baz)// - >恐慌:运行时错误:无效的内存地址或零指针解引用
fmt.Println(f1.Bar.Baz.Baz)// - >恐慌:运行时错误:无效的内存地址或零指针解引用
//到目前为止这么好,但是

//是否有更通用的方法来执行此类测试?
if f2.Bar!= nil&& f2.Bar.Baz!= nil {
fmt.Println(f2.Bar.Baz.Baz)
} else {
fmt.Println(something nil)
}
}

问题是如果有更通用的方法来测试某个节点是否在参考树是零?我需要得到很多不同的项目,并且写下所有这些if语句会很痛苦。
呵呵,速度值得关注;) 解决方案

如果你想避免' reflect '(反射,作为通用方式来测试字段任何 struct ,有点像 Golang - 使用反射获取指针值或在这个要点:它比较慢),最可靠的方法是在 Foo 上实现方法,以便返回正确的值

  func(foo * Foo)BarBaz()string {
if f2.Bar!= nil&& f2.Bar.Baz!= nil {
return f2.Bar.Baz.Baz
} else {
fmt.Println(something nil)
return//例如


code
$ b

如果有很多写这些函数,也许去1.4 去生成 命令可以帮助生成其中的大部分。


I have a deeply nested struct in go. These are constructed by a json unmarshaller.

Quite some fields in this struct are however 'omitifempty' so I end op with a struct that can have nills in various places.

Example (the real thing is even deeper nested, and big: 400 lines of structs):

package main

import "fmt"

type Foo struct {
    Foo string
    Bar *Bar
}

type Bar struct {
    Bar string
    Baz *Baz
}

type Baz struct {
    Baz string
}

func main() {
    f1 := Foo{Foo: "f1"}
    f2 := Foo{Foo: "f2", Bar: &Bar{Bar: "br2"}}
    f3 := Foo{Foo: "f3", Bar: &Bar{Bar: "br3", Baz: &Baz{Baz: "bz3"}}}

    fmt.Println(f3.Bar.Baz.Baz) //-> bz3
    fmt.Println(f2.Bar.Baz.Baz) //-> panic: runtime error: invalid memory address or nil pointer dereference
    fmt.Println(f1.Bar.Baz.Baz) //-> panic: runtime error: invalid memory address or nil pointer dereference    
    //so far so good, but

    //is there a more generic way to do this kind of testing?
    if f2.Bar != nil && f2.Bar.Baz != nil {
        fmt.Println(f2.Bar.Baz.Baz)
    } else {
        fmt.Println("something nil")
    }
}

The question is if there is a more generic way to test if some node in the reference tree is nil? I need to get a lot of different items and it will be a pain to write all these if statements. Oh and speed is of concern ;)

解决方案

If you want to avoid 'reflect' (reflection, as a "generic" way to test fields of any struct, a bit as in "Golang - Get pointer to value using reflection" or in this gist: it is slower), the surest way would be to implement methods on Foo in order to return the right value

func (foo *Foo) BarBaz() string {
    if f2.Bar != nil && f2.Bar.Baz != nil {
        return f2.Bar.Baz.Baz
    } else {
        fmt.Println("something nil")
        return "" // for example
    } 
}

If there are a lot of such functions to write, maybe go 1.4 go generate command can help generate most of them.

这篇关于测试golang嵌套结构中的零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆