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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。