Reputation: 795
I’ve the following code which needs to get int value and add it to a string with string suffix. E.g.
At start I'm getting this
"fds data "
After the if
statement it should like this
"fds data 10 M"
This is the code:
ltrCfg := "fds data "
if len(cfg.ltrSharedDicts) > 0 {
ltrCfg += strconv.Itoa(cfg.ltrSharedDicts["c_data"])
ltrCfg += "M"
} else {
ltrCfg += "10M"
}
out = append(out, ltrCfg)
ltrCert := “fds data "
if len(cfg.ltrSharedDicts) > 0 {
ltrCert += strconv.Itoa(cfg.ltrSharedDicts["d_data"])
ltrCert += "M"
} else {
ltrCert += “20M"
}
out = append(out, ltrCert)
The code is working but I wonder for the first fork of the if statement
if len(cfg.ltrSharedDicts) > 0 {
ltrCfg += strconv.Itoa(cfg.ltrSharedDicts["c_data"])
ltrCfg += "M"
Is there a better way to achieve it?
Upvotes: 0
Views: 463
Reputation: 166626
For readability, I would write:
cd, ok := cfg.ltrSharedDicts["c_data"]
if !ok {
cd = 10
}
out = append(out, fmt.Sprintf("fds data %dM", cd))
Upvotes: 1