I’ve the following function which needs to provide a strings with data in two options
- Value from config is provided ( can change only the values of
7MB
or3MB
) all the prefix is the same - If config not provided fallback to defaults which is
7MB
and3MB
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, ";
") + ";"
}