duanjianhe1388 2017-10-03 04:03 采纳率: 100%
浏览 36
已采纳

如何检查结构的特定属性是否为空?

Forgive me, I come from a c# background!

I have following struct in Go. We populate this struct by reading in the config from a file, which works well. However Im trying to find out a way to tell if a particular property in the struct, when passed in through the config file is null. As in, explicitly not set at all.

I've struggled for about 3 hours on this. I can do it for type strings etc, but I can't find out how to do it generically, across all types?

package main

import (
    "encoding/json"
    "fmt"
    "os"
    "reflect"
)

// Config type for configuration
type Config struct {
    BatchSize  int    `json:"batchSize"`
    BatchTime  int    `json:"batchTime"`
    DataFolder string `json:"dataFolder"`
    TempFolder string `json:"tempFolder"`

    //Kafka configuration
    Brokers      []string `json:"streamBrokers"`
    TopicJoined  string   `json:"streamTopicJoined"`
    TopicRemoved string   `json:"streamTopicRemoved"`
    Group        string   `json:"streamGroup"`
    ClientName   string   `json:"streamClientName"`

    // Stats configuration
    StatsPrefix string `json:"statsPrefix"`

    //AWS S3 configuration
    AccessKey              string `json:"amazonAccessKey"`
    SecretKey              string `json:"amazonSecretKey"`
    Region                 string `json:"amazonRegion"`
    Endpoint               string `json:"amazonEndpoint"`
    S3Bucket               string `json:"amazonS3Bucket"`
    S3UploadBufferSize     int32  `json:"amazonS3UploadBufferSize"`
    S3UploadConcurrentSize int32  `json:"amazonS3UploadConcurrentSize"`
    S3UploadRetries        int32  `json:"amazonS3UploadRetries"`
    S3UploadRetryTime      int32  `json:"amazonS3UploadRetryTime"`

    //Logging
    StatsdHost string  `json:"statsdHost"`
    StatsdPort int     `json:"statsdPort"`
    StatsdRate float64 `json:"statsdRate"`

    //Test Publishing
    TestMode  bool `json:"testMode"`
    TestCount int  `json:"testCount"`
}

// LoadConfig load config from file
func LoadConfig(configFile string) *Config {
    if _, err := os.Stat(configFile); os.IsNotExist(err) {
        panic(err)
    }

    if config, err := loadFromFile(configFile); nil != err {
        panic(err)
    } else {
        fmt.Println("OneDrive", os.Getenv("OneDrive"))
        msValuePtr := reflect.ValueOf(config)
        msValue := msValuePtr.Elem()
        typeOfT := msValue.Type()

        for i := 0; i < msValue.NumField(); i++ {
            field := msValue.Field(i)
            // TODO: Check if field is null, regardless of type and the value from OS env variables...

        }
        return config
    }
}

func loadFromFile(path string) (*Config, error) {
    var config Config
    file, err := os.Open(path)
    if err != nil {
        return nil, fmt.Errorf("could not open config path %q: %v", path, err)
    }
    defer file.Close()

    decoder := json.NewDecoder(file)
    err = decoder.Decode(&config)
    if err != nil {
        return nil, fmt.Errorf("could not parse config path %q: %v", path, err)
    }
    return &config, nil
}
  • 写回答

1条回答 默认 最新

  • duanao2688 2017-10-03 04:16
    关注

    In go, a value's default value is its zero value. You may want to make all of your types pointers (eg: *string rather than string) since the zero value of a pointer is nil. Unmarshaling your config file into a struct would preserve nil values for the keys that are missing / have null values.

    Note that since slices (eg: []string) are reference types, they act as pointers and are nullable (meaning you wouldn't need to declare the type as *[]string).

    I've used this library in the past to help with merging config / setting required keys (and many others exist): https://github.com/jinzhu/configor

    Example of encoding/decoding json - https://play.golang.org/p/DU_5Tuvm5-

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?