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

时间:2018-12-09 19:28:15

标签: go

我正在从API检索数据。 结构输出为:

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

但是,如果API输出中没有StreamSpecification,则尝试打印该结构时会收到以下错误。

紧急:运行时错误:无效的内存地址或nil指针取消引用 [signal SIGSEGV:细分违规代码= 0x1 addr = 0x0 pc = xxxxxxxx]

如何检查值中是否存在结构StreamSpecification?或如何以其他方式解决该问题?

2 个答案:

答案 0 :(得分:2)

如果我对问题的理解正确,我会将结构转换为地图,然后检查您感兴趣的字段是否在地图中。

例如:

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

答案 1 :(得分:0)

听起来好像您是在尝试不首先确认JSON对象中是否提供decoder_input = Input(shape=(encoding_dim,)) decoder = Model(decoder_input, autoencoder.layers[-1](decoder_input)) 时尝试访问StreamEnabledStreamViewType

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

nil
相关问题