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

I am retrieving data from an API. The struct output is :

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

But if the API output does not have StreamSpecification in it, I am receiving the following error when trying to print the struct.

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

How to check if the struct StreamSpecification exists in the value? Or how to fix the issue in any other way?

If I understand the question correctly, I would convert the struct into a map, and then check if the field you are interested in is in the map.

For example:

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
", ok)                                                                                       

    _, ok = myMap["Location"]                                                                                                      
    fmt.Printf("Location is in myMap: %t
", ok)                                                                                   

} 

Go Playground

It sounds like you're trying to access StreamEnabled or StreamViewType when you haven't first confirmed if StreamSpecification was provided in the JSON object.

Assuming you have the internal StreamSpecification as a reference to a struct, you need to ensure that StreamSpecification isn't nil:

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