如何使用索引打印内容?

huangapple go评论162阅读模式
英文:

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

Package fmt

显式参数索引:

在 Printf、Sprintf 和 Fprintf 中,默认行为是每个格式化动词按照调用中传递的连续参数进行格式化。然而,[n] 表示在动词之前的第 n 个一索引参数将被格式化。在宽度或精度的 '*' 前面使用相同的表示法,可以选择保存该值的参数索引。在处理了一个带括号的表达式 [n] 后,后续的动词将使用参数 n+1、n+2 等,除非另有指示。

例如,

fmt.Sprintf("%[2]d %[1]d\n", 11, 22)

将得到 "22 11"

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf 根据格式说明符进行格式化,并写入 w。它返回写入的字节数和遇到的任何写入错误。

func Printf

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.Writeror 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"

huangapple
  • 本文由 发表于 2015年10月29日 21:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/33415745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定