首页 文章

如何检查结构中是否存在struct值

提问于
浏览
-2

我正在从API检索数据 . struct输出是:

{
    StreamSpecification: {
      StreamEnabled: true,
      StreamViewType: "NEW_AND_OLD_IMAGES"
     },
    TableStatus: "ACTIVE"
  }

但是如果API输出中没有StreamSpecification,我在尝试打印结构时收到以下错误 .

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=xxxxxxxx]

如何检查值StreamSpecification是否存在于值中?或者如何以任何其他方式解决问题?

2 回答

  • 2

    如果我正确理解了这个问题,我会将结构转换为 Map ,然后检查您感兴趣的字段是否在 Map 中 .

    例如:

    package main                                                                                                                       
    
    import (                                                                                                                           
        "encoding/json"                                                                                                                
        "fmt"                                                                                                                          
    )                                                                                                                                  
    
    type MyStruct struct {                                                                                                             
        Name  string                                                                                                                   
        Score int                                                                                                                      
    }                                                                                                                                  
    
    func main() {                                                                                                                      
    
        ms := MyStruct{Name: "Amy", Score: 34}                                                                                         
    
        var myMap map[string]interface{}                                                                                               
        data, _ := json.Marshal(ms)                                                                                                    
        fmt.Println(data)                                                                                                              
    
        json.Unmarshal(data, &myMap)                                                                                                   
    
        fmt.Println(myMap)                                                                                                             
    
        _, ok := myMap["Name"]                                                                                                         
        fmt.Printf("name is in myMap: %t\n", ok)                                                                                       
    
        _, ok = myMap["Location"]                                                                                                      
        fmt.Printf("Location is in myMap: %t\n", ok)                                                                                   
    
    }
    

    Go Playground

  • 0

    听起来好像您在第一次确认JSON对象中是否提供了 StreamSpecification 时,是否正在尝试访问 StreamEnabledStreamViewType .

    假设您有内部 StreamSpecification 作为结构的引用,您需要确保 StreamSpecification 不是 nil

    if (instance.StreamSpecification == nil) {
        // StreamSpecification was not passed in the JSON.
    }
    

相关问题