dongzhang8680 2019-06-15 06:55
浏览 22
已采纳

使用配置中的值更新字符串数组

I’ve the following function which needs to provide a strings with data in two options

  1. Value from config is provided ( can change only the values of 7MB or 3MB ) all the prefix is the same
  2. If config not provided fallback to defaults which is 7MB and 3MB

example

if I dont get any value from the config API the function should return default values, which is hard-coded in the function. like this:

“l_sh_dit conf_data 7MB
 l_sh_dit cert_data 3MB”

if im getting value from the config app let say 1MB (conf) and 100MB (cert) respectively the output of the function should look l

“l_sh_dit conf_data 1MB
 l_sh_dit cert_data 100MB”

The problem is that I’ve already the defaults value hard-coded, how can I update in each element in the array only the last value (the number 10M or all other value) , in efficient way ?

I've tried with string builder API without success as this is a bit tricky, what could be missing here?

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, ";
") + ";"

}
  • 写回答

1条回答 默认 最新

  • dsf5989 2019-06-15 18:51
    关注

    One of the reasons your program is failing is because you are copying a non-zero strings.Builder instance which is disallowed.

    A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.

    Also, and I may be wrong here, but I believe the builder is intended to provide better performance for when dealing with complex enough rules where the use of numerous individual steps to build up the string is justified. Yours is not one of those cases, however, because all you need is a single concatenation to get what you want.

    type Cfg struct { A, B string }
    
    func getShCfg(c Cfg) string {
        var out []string
    
        la := "l_sh_dit conf_data 7MB"
        lb := "l_sh_dit cert_data 3MB"
    
        if len(c.A) > 0 {
            out = append(out, "l_sh_dit conf_data " + c.A)
        } else {
            out = append(out, la)
        }
        if len(c.B) > 0 {
            out = append(out, "l_sh_dit cert_data "+c.B)
    
        } else {
            out = append(out, lb)
        }
    
        return strings.Join(out, ";
    ") + ";"
    
    }
    

    https://play.golang.com/p/WKMXxCXGuTt


    Additionally if the number of the config properties grows and you want to avoid writing too many if-elses then you can always extract that logic into a func.

    func getCfgProp(key, val, def string) string {
        if len(val) > 0 {
            return key + " " + val
        }
        return key + " " + def
    }
    

    https://play.golang.com/p/daXqV4-Umza

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加