go - 在 Go 中使用星号 (*) 指定宽度和精度

标签 go

golang.org 的官方 fmt pacakge 文档在指定宽度和精度方面这样说 -

Either or both of the flags may be replaced with the character '*', causing their values to be obtained from the next operand, which must be of type int.

星号 (*) 将如何使用,例如,在以下场景中 -

 var f float32 = 12.3456789
 fmt.Printf("%6.3f", f)

最佳答案

应该是

var f float32 = 12.3456789
fmt.Printf("%*.*f", 20, 1, f)

查看更多:

关于go - 在 Go 中使用星号 (*) 指定宽度和精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47707234/

相关文章:

dictionary - go 的 map 哈希函数如何工作,具有 "same"值的不同类型会导致不同的键?

go - 如何为 ListenAndServeTLS 功能提供 CA 证书

mongodb - slice 元素的查找`from`集合上的match字段不起作用

concurrency - 什么时候可以在不锁定的情况下安全地访问受互斥锁保护的变量?

go - 多个串行请求导致缓冲区为空

api - 使用 golang 服务器的第三方 API 的速率限制

go - 当我的通配符参数中有双斜杠时,为什么 Gorilla mux 会重定向?

go - 执行模板到文件

xml - 在go中用xml节点名序列化int

go - 如何在go中删除结构对象?