?
This document uses PHP Chinese website manual Release
import "text/tabwriter"
概觀
索引
示例
Package tabwriter實(shí)現(xiàn)了一個(gè)寫入過濾器(tabwriter.Writer),它將輸入中的選項(xiàng)卡式列轉(zhuǎn)換為正確對(duì)齊的文本。
文本/制表程序包被凍結(jié),并且不接受新功能。
package mainimport ("fmt""os""text/tabwriter")func main() {// Observe how the b's and the d's, despite appearing in the// second cell of each line, belong to different columns. w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, '.', tabwriter.AlignRight|tabwriter.Debug) fmt.Fprintln(w, "a\tb\tc") fmt.Fprintln(w, "aa\tbb\tcc") fmt.Fprintln(w, "aaa\t") // trailing tab fmt.Fprintln(w, "aaaa\tdddd\teeee") w.Flush()}
package mainimport ("fmt""os""text/tabwriter")func main() {// Observe that the third line has no trailing tab,// so its final cell is not part of an aligned column.const padding = 3 w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, '-', tabwriter.AlignRight|tabwriter.Debug) fmt.Fprintln(w, "a\tb\taligned\t") fmt.Fprintln(w, "aa\tbb\taligned\t") fmt.Fprintln(w, "aaa\tbbb\tunaligned") // no trailing tab fmt.Fprintln(w, "aaaa\tbbbb\taligned\t") w.Flush()}
常量
鍵入Writer
func NewWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer
func (b *Writer) Flush() error
func (b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer
func (b *Writer) Write(buf []byte) (n int, err error)
Writer.Init Package (Elastic) Package (TrailingTab)
tabwriter.go
格式化可以用這些標(biāo)志進(jìn)行控制。
const ( // Ignore html tags and treat entities (starting with '&' // and ending in ';') as single characters (width = 1). FilterHTML uint = 1 << iota // Strip Escape characters bracketing escaped text segments // instead of passing them through unchanged with the text. StripEscape // Force right-alignment of cell content. // Default is left-alignment. AlignRight // Handle empty columns as if they were not present in // the input in the first place. DiscardEmptyColumns // Always use tabs for indentation columns (i.e., padding of // leading empty cells on the left) independent of padchar. TabIndent // Print a vertical bar ('|') between columns (after formatting). // Discarded columns appear as zero-width columns ("||"). Debug)
要轉(zhuǎn)義文本段,請(qǐng)使用轉(zhuǎn)義字符進(jìn)行包圍。例如,此字符串中的選項(xiàng)卡“忽略此選項(xiàng)卡: \xff\t\xff ”不會(huì)終止單元格,并且為了格式化目的而構(gòu)成寬度為1的單個(gè)字符。
值 0xff 被選中是因?yàn)樗荒芤杂行У?UTF-8 序列出現(xiàn)。
const Escape = '\xff'
Writer是一個(gè)過濾器,用于在其輸入中的制表符分隔列中插入填充以將其與輸出對(duì)齊。
Writer將傳入的字節(jié)視為由水平 ('\t') 或垂直 ('\v') 制表符以及換行符 ('\n') 或換頁符 ('\f') 組成的單元組成的 UTF-8 編碼文本。字符; 換行符和換頁符都是換行符。
連續(xù)行中制表符終止的單元格構(gòu)成列。 Writer 根據(jù)需要插入填充以使列中的所有單元格具有相同的寬度,從而有效地對(duì)齊列。它假定所有字符都具有相同的寬度,但必須指定 tabwidth 的制表符除外。列單元格必須是制表符終止的,而不是制表符分隔的:行結(jié)尾處的非制表符終止的結(jié)尾文本形成單元格,但該單元格不是對(duì)齊列的一部分。例如,在本例中(其中|代表水平制表符):
aaaa|bbb|d aa |b |dd a |aa |cccc|eee
b 和 c 在不同的列中(b 列在所有方面都不是連續(xù)的)。d 和 e 根本不在列中(沒有終止標(biāo)簽,列也不是連續(xù)的)。
Writer 假定所有的 Unicode 代碼點(diǎn)具有相同的寬度;在某些字體中或者字符串包含組合字符時(shí),這可能不是真的。
如果設(shè)置了 DiscardEmptyColumns ,則會(huì)放棄完全由垂直(或“軟”)選項(xiàng)卡終止的空列。水平(或“硬”)選項(xiàng)卡終止的列不受此標(biāo)志的影響。
如果 Writer 配置為過濾 HTML ,則會(huì)傳遞 HTML 標(biāo)記和實(shí)體。為了格式化目的,標(biāo)簽和實(shí)體的寬度被假定為零(標(biāo)簽)和一個(gè)(實(shí)體)。
一段文字可以通過用 Escape 字符括起來進(jìn)行轉(zhuǎn)義。制表人通過不變的方式傳遞轉(zhuǎn)義文本段。特別是,它不解釋段中的任何制表符或換行符。如果設(shè)置了 StripEscape 標(biāo)志,則 Escape 字符將從輸出中剝離; 否則它們也會(huì)通過。為了格式化,轉(zhuǎn)義字符的寬度始終計(jì)算,不包括轉(zhuǎn)義字符。
換頁字符就像換行符,但它也會(huì)終止當(dāng)前行中的所有列(有效地調(diào)用 Flush )。下一行中以制表符結(jié)尾的單元格開始新列。除非在 HTML 標(biāo)簽內(nèi)或轉(zhuǎn)義文本段內(nèi)發(fā)現(xiàn),否則換頁字符在輸出中顯示為換行符。
寫入器必須在內(nèi)部緩沖輸入,因?yàn)橐粭l線的適當(dāng)間距可能取決于未來線中的單元。完成調(diào)用寫入后,客戶端必須調(diào)用 Flush 。
type Writer struct { // contains filtered or unexported fields}
func NewWriter(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer
NewWriter 分配并初始化一個(gè)新的 Tabwriter.Writer 。參數(shù)與 Init 函數(shù)相同。
func (b *Writer) Flush() error
在上次調(diào)用 Write 之后應(yīng)該調(diào)用 Flush ,以確保寫入器中緩沖的任何數(shù)據(jù)都寫入輸出。出于格式化目的,任何不完整的轉(zhuǎn)義序列都視為完成。
func (b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar byte, flags uint) *Writer
寫入器必須通過對(duì) Init 的調(diào)用進(jìn)行初始化。第一個(gè)參數(shù)(輸出)指定了過濾器輸出。其余參數(shù)控制格式:
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 paddingif 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 mainimport ("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()}
func (b *Writer) Write(buf []byte) (n int, err error)
寫入器寫 buf 給寫入器 b 。返回的唯一錯(cuò)誤是在寫入底層輸出流時(shí)遇到的錯(cuò)誤。