0

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?

2 Answers 2

3

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

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

} 

Go Playground

Sign up to request clarification or add additional context in comments.

Comments

-1

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.
}

2 Comments

I had tried that but i am getting a compilation error "cannot convert nil to type dynamodb.StreamSpecification" Here is the struct docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/…
Can you just include a minimal reproducible example in your question - hard to diagnose if we can't see your actual code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.