我有以下函数,需要在两个选项中提供带有数据的字符串
提供了配置中的值(只能更改7MB
或 的值3MB
)所有前缀都是相同的
如果未提供配置,则回退到默认值,7MB
并且3MB
例子
如果我没有从配置 API 中获取任何值,该函数应该返回默认值,该值是硬编码在函数中的。像这样:
“l_sh_dit conf_data 7MB l_sh_dit cert_data 3MB”
如果我从应用程序中获得价值,config
假设分别为 1MB(conf)和 100MB(cert),则该函数的输出应该看起来像 l
“l_sh_dit conf_data 1MB l_sh_dit cert_data 100MB”
问题是我已经有了defaults
值hard-coded
,如何才能element
有效地更新数组中的每个值中的最后一个值(数字 10M 或所有其他值)?
我尝试过使用字符串生成器 API,但没有成功,因为这有点棘手,这里可能缺少什么?
func getShCfg() string{
var out []string
var b1 strings.Builder
la:= "l_sh_dit conf_data 7MB"
lb := "l_sh_dit cert_data 3MB"
cfg, ok := c.(config.Configuration)
if !ok {
log.Errorf(“error:”, c)
return ""
}
// here I got the data from config, else fallback to defaults
if len(cfg.A) > 0 {
// HERE is my problem which doesn’t works
b1.WriteString("l_sh_dit")
b2 := b1
b2.WriteString("conf_data")
b3 := b2
b3.WriteString(cfg.A)
out = append(out, b3)
} else {
out = append(out, la)
}
// here I got the data from config, else fallback to defaults
if len(cfg.B) > 0 {
// HERE is my problem which doesn’t works
b1.WriteString("l_sh_dit")
b2 := b1
b2.WriteString("cert_data")
b3 := b2
b3.WriteString(cfg.B)
out = append(out, b3)
} else {
out = append(out, lb)
}
return strings.Join(out, ";\n\r") + ";"
}
繁花如伊
相关分类