英文:
How to print using indexes?
问题
有没有一种方法可以使用变量索引进行打印?
fmt.Fprintf("%[1] %[2] %[3] %[4]", a, b, c, d)
我得到了关于“字符串没有实现io.Writer”的错误。
使用fmt.Println
会将变量索引打印为字面值。
英文:
Is there a way to print using variable indexes?
fmt.Fprintf("%[1] %[2] %[3] %[4]", a, b, c, d)
I get errors about
> string does not implement io.Writer
Using fmt.Println
prints the variable indexes as a literal.
答案1
得分: 8
显式参数索引:
在 Printf、Sprintf 和 Fprintf 中,默认行为是每个格式化动词按照调用中传递的连续参数进行格式化。然而,[n] 表示在动词之前的第 n 个一索引参数将被格式化。在宽度或精度的 '*' 前面使用相同的表示法,可以选择保存该值的参数索引。在处理了一个带括号的表达式 [n] 后,后续的动词将使用参数 n+1、n+2 等,除非另有指示。
例如,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
将得到 "22 11"
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
Fprintf 根据格式说明符进行格式化,并写入 w。它返回写入的字节数和遇到的任何写入错误。
func Printf(format string, a ...interface{}) (n int, err error)
Printf 根据格式说明符进行格式化,并写入标准输出。它返回写入的字节数和遇到的任何写入错误。
对于 Fprintf
,提供一个 io.Writer
或使用 Printf
。此外,将格式说明符中的 'verbs' 添加到你的格式说明符中。例如,
package main
import (
"fmt"
"os"
)
func main() {
a, b, c, d := 1, 2, 3, 4
fmt.Fprintf(os.Stdout, "%[1]d %[2]d %[3]d %[4]d\n", a, b, c, d)
fmt.Printf("%[1]d %[2]d %[3]d %[4]d\n", a, b, c, d)
}
输出:
1 2 3 4
1 2 3 4
英文:
> Package fmt
>
> Explicit argument indexes:
>
> In Printf, Sprintf, and Fprintf, the default behavior is for each
> formatting verb to format successive arguments passed in the call.
> However, the notation [n] immediately before the verb indicates that
> the nth one-indexed argument is to be formatted instead. The same
> notation before a '*' for a width or precision selects the argument
> index holding the value. After processing a bracketed expression [n],
> subsequent verbs will use arguments n+1, n+2, etc. unless otherwise
> directed.
>
> For example,
>
> fmt.Sprintf("%2d %1d\n", 11, 22)
>
> will yield "22 11"
>
> func Fprintf
>
> func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
>
> Fprintf formats according to a format specifier and writes to w. It
> returns the number of bytes written and any write error encountered.
>
> func Printf
>
> func Printf(format string, a ...interface{}) (n int, err error)
>
> Printf formats according to a format specifier and writes to standard
> output. It returns the number of bytes written and any write error
> encountered.
For Fprintf
provide an io.Writer
or use Printf
. Also, add format 'verbs' to your format specifier. For example,
package main
import (
"fmt"
"os"
)
func main() {
a, b, c, d := 1, 2, 3, 4
fmt.Fprintf(os.Stdout, "%[1]d %[2]d %[3]d %[4]d\n", a, b, c, d)
fmt.Printf("%[1]d %[2]d %[3]d %[4]d\n", a, b, c, d)
}
Output:
1 2 3 4
1 2 3 4
答案2
得分: 2
fmt.Fprintf有如下定义:
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
所以你需要提供一个写入器(writer)。
对于索引,你可以使用以下格式:"%[2]d %[1]d"
英文:
fmt.Fprintf has definition:
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
So you need to provide writer.
For indexing you can use format like this: "%[2]d %[1]d"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论