当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


GO Writer.Init用法及代码示例


GO语言"text/tabwriter"包中"Writer.Init"类型的用法及代码示例。

用法:

func(b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer

必须通过调用 Init 来初始化 Writer。第一个参数(输出)指定过滤器输出。其余参数控制格式:

minwidth	minimal cell width including any padding
tabwidth	width of tab characters (equivalent number of spaces)
padding		padding added to a cell before computing its width
padchar		ASCII char used for padding
		if padchar == '\t', the Writer will assume that the
		width of a '\t' in the formatted output is tabwidth,
		and cells are left-aligned independent of align_left
		(for correct-looking results, tabwidth must correspond
		to the tab width in the viewer displaying the result)
flags		formatting control

例子:

package main

import (
	"fmt"
	"os"
	"text/tabwriter"
)

func main() {
	w := new(tabwriter.Writer)

	// Format in tab-separated columns with a tab stop of 8.
	w.Init(os.Stdout, 0, 8, 0, '\t', 0)
	fmt.Fprintln(w, "a\tb\tc\td\t.")
	fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.")
	fmt.Fprintln(w)
	w.Flush()

	// Format right-aligned in space-separated columns of minimal width 5
	// and at least one blank of padding (so wider column entries do not
	// touch each other).
	w.Init(os.Stdout, 5, 0, 1, ' ', tabwriter.AlignRight)
	fmt.Fprintln(w, "a\tb\tc\td\t.")
	fmt.Fprintln(w, "123\t12345\t1234567\t123456789\t.")
	fmt.Fprintln(w)
	w.Flush()

}

输出:

a	b	c	d		.
123	12345	1234567	123456789	.

    a     b       c         d.
  123 12345 1234567 123456789.

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Writer.Init。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。