doule6314 2019-06-07 18:32
浏览 34
已采纳

我想格式化一个整数数组。 如何发送数组,以便每个动词接收数组的不同元素?

I have a variable array of integers that I would like to format into a string. When I pass the array, it is placed into the first format verb and the rest are listed as missing. For example, I pass [10, 100, 250, 99] and a format of "%02d-%04d-%04d-%02d". I want it to return "10-0100-0250-99". What is the correct way to do this in go?

package main

include (
    "fmt"
)

func main() {
    nums := []int{10,100,250,99}
    format := "%02d-%04d-%04d-%02d
"

    fmt.Printf(format, 10, 100, 250, 99)
    fmt.Printf(format, nums)
}

[10 100 250 99]-%!d(MISSING)-%!d(MISSING)-%!d(MISSING) (Wrong) 10-0100-0250-99 (correct)

  • 写回答

1条回答 默认 最新

  • dravpuso44681 2019-06-07 18:38
    关注

    You either need to pass in the elements individually:

    nums := []int{10,100,250,99}
    format := "%02d-%04d-%04d-%02d
    "
    fmt.Printf(format, nums[0], nums[1], nums[2], nums[3])
    

    Or use the correct type for the fmt.Printf argument, which is []interface{}:

    nums := []interface{}{10,100,250,99}
    format := "%02d-%04d-%04d-%02d
    "
    fmt.Printf(format, nums...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧