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

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

}

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