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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端