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...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路