donglilian0061 2016-05-03 03:45
浏览 22
已采纳

Golang常数结构键

In PHP we can do something like:

if ($env == "dev") 
  define("key", "key")
else
  define("key", "secret")

// json ouput
//{ key : "value" } or { secret : "value" }

How do I properly translate above PHP approach to GO?

I was thinking of something like:

if *env == "dev" {
  type response struct {
    key string
    ...50 more keys that should also be different depending on env
  }
} else {
    secret string
    ...50 more keys...
}

But I guess that is not only wrong but would also create a huge repeated code...

  • 写回答

3条回答 默认 最新

  • dongyun4010 2016-05-03 05:36
    关注

    You may create a struct type holding the common part of your data structure, and you may create new types embedding this and only adding the deviating new field. So there is no code repetition of the common part of the data structure:

    type Response struct {
        F1 string
        F2 int
    }
    
    func main() {
        for _, env := range []string{"dev", "prod"} {
            if env == "dev" {
                type Resp struct {
                    Response
                    Key string
                }
                r := Resp{Response{"f1dev", 1}, "value"}
                json.NewEncoder(os.Stdout).Encode(r)
            } else {
                type Resp struct {
                    Response
                    Secret string
                }
                r := Resp{Response{"f1pro", 2}, "value"}
                json.NewEncoder(os.Stdout).Encode(r)
            }
        }
    }
    

    Output (try it on the Go Playground):

    {"F1":"f1dev","F2":1,"Key":"value"}
    {"F1":"f1pro","F2":2,"Secret":"value"}
    

    Note that you may also use the same Response value if they are the same for the 2 use cases:

    comResp := Response{"f1value", 1}
    if env == "dev" {
        type Resp struct {
            Response
            Key string
        }
        r := Resp{comResp, "value"}
        json.NewEncoder(os.Stdout).Encode(r)
    } else {
        type Resp struct {
            Response
            Secret string
        }
        r := Resp{comResp, "value"}
        json.NewEncoder(os.Stdout).Encode(r)
    }
    

    You may shorten the code above by using anonymous struct and not creating a local variable (not necessarily more readable):

    if env == "dev" {
        json.NewEncoder(os.Stdout).Encode(struct {
            Response
            Key string
        }{comResp, "value"})
    } else {
        json.NewEncoder(os.Stdout).Encode(struct {
            Response
            Secret string
        }{comResp, "value"})
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上