简体   繁体   English

用int连接字符串

[英]GO concatenate string with int

I've the following code which needs to get int value and add it to a string with string suffix. 我有以下代码需要获取int值并将其添加到带有字符串后缀的字符串中。 Eg 例如

At start I'm getting this 一开始我得到这个

"fds data "

After the if statement it should like this if语句之后,它应该像这样

"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语句的第一个分支

if len(cfg.ltrSharedDicts) > 0 {
    ltrCfg += strconv.Itoa(cfg.ltrSharedDicts["c_data"])
    ltrCfg += "M"

Is there a better way to achieve it? 有没有更好的方法来实现呢?

For readability, I would write: 为了提高可读性,我会写:

cd, ok := cfg.ltrSharedDicts["c_data"]
if !ok {
    cd = 10
}
out = append(out, fmt.Sprintf("fds data %dM", cd))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM