Golang的tabwriter打印出了垃圾字符,并且格式化不正确。

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

golang tabwriter printing junk and not formatting properly

问题

我正在尝试使用tabwriter打印帮助信息:

func printHelp() {
	writer := tabwriter.NewWriter(os.Stdout, 100, 8, 1, ' ', 0)
	fmt.Fprintln(writer, "uni outputs Unicode information for characters.")
	fmt.Fprintln(writer, "USAGE:")
	fmt.Fprintln(writer, "  uni <input>\tOutputs Unicode info for the input.\t")
	fmt.Fprintln(writer, "\tProcesses U+xxxx sequences into the appropriate characters and treats control characters as control.\t")
	fmt.Fprintln(writer, "  uni raw <input>\tOutputs Unicode info on the raw unporocessed input.\t")
	fmt.Fprintln(writer, "  uni decomp <input\tOutputs the input with all characters decomposed.\t")
	fmt.Fprintln(writer, "  uni comp <input>\tOutputs the input with all characters composed.\t")
	fmt.Fprintln(writer, "  uni short <input>\tOutputs basic info about the input, one character per line.\t")
	fmt.Fprintln(writer, "  uni rawshort <input>\tOutputs basic info about the raw unprocessed input, one character per line.\t")
	fmt.Fprintln(writer, "  uni help\tPrints this help message.\t")
	writer.Flush()
}

然而,输出结果包含了一些不应该打印的内部垃圾信息,并且列没有正确对齐。

这是我得到的结果:

&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} uni outputs Unicode information for characters.
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} USAGE:
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni <input>	Outputs Unicode info for the input.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} 	Processes U+xxxx sequences into the appropriate characters and treats control characters as control.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni raw <input>	Outputs Unicode info on the raw unporocessed input.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni decomp <input	Outputs the input with all characters decomposed.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni comp <input>	Outputs the input with all characters composed.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni short <input>	Outputs basic info about the input, one character per line.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni rawshort <input>	Outputs basic info about the raw unprocessed input, one character per line.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni help	Prints this help message.	

这是大致上我想要的结果:

uni outputs Unicode information for characters.
USAGE:
  uni <input>           Outputs Unicode info for the input.
                        Processes U+xxxx sequences into the appropriate characters and treats control characters as control.
  uni raw <input>       Outputs Unicode info on the raw unporocessed input.
  uni decomp <input     Outputs the input with all characters decomposed.
  uni comp <input>      Outputs the input with all characters composed.
  uni short <input>     Outputs basic info about the input, one character per line.
  uni rawshort <input>  Outputs basic info about the raw unprocessed input, one character per line.
  uni help              Prints this help message.

我尝试过将最小单元格宽度更改为各种值,从0到几百,它改变了对齐方式,但没有正确工作。我尝试过制表符和空格分隔符,以及右对齐和默认(左)对齐方式,但没有任何区别。

英文:

I'm trying to print a help message using tabwriter:

func printHelp() {
writer := tabwriter.NewWriter(os.Stdout, 100, 8, 1, &#39; &#39;, 0)
fmt.Println(writer, &quot;uni outputs Unicode information for characters.&quot;)
fmt.Println(writer, &quot;USAGE:&quot;)
fmt.Println(writer, &quot;  uni &lt;input&gt;\tOutputs Unicode info for the input.\t&quot;)
fmt.Println(writer, &quot;\tProcesses U+xxxx sequences into the appropriate characters and treats control characters as control.\t&quot;)
fmt.Println(writer, &quot;  uni raw &lt;input&gt;\tOutputs Unicode info on the raw unporocessed input.\t&quot;)
fmt.Println(writer, &quot;  uni decomp &lt;input\tOutputs the input with all characters decomposed.\t&quot;)
fmt.Println(writer, &quot;  uni comp &lt;input&gt;\tOutputs the input with all characters composed.\t&quot;)
fmt.Println(writer, &quot;  uni short &lt;input&gt;\tOutputs basic info about the input, one character per line.\t&quot;)
fmt.Println(writer, &quot;  uni rawshort &lt;input&gt;\tOutputs basic info about the raw unprocessed input, one character per line.\t&quot;)
fmt.Println(writer, &quot;  uni help\tPrints this help message.\t&quot;)
writer.Flush()
}

However the output both contains a significant amount of internal junk that shouldn't be printed and isn't aligning the columns correctly.

Here's what I get:

&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} uni outputs Unicode information for characters.
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} USAGE:
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni &lt;input&gt;	Outputs Unicode info for the input.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} 	Processes U+xxxx sequences into the appropriate characters and treats control characters as control.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni raw &lt;input&gt;	Outputs Unicode info on the raw unporocessed input.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni decomp &lt;input	Outputs the input with all characters decomposed.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni comp &lt;input&gt;	Outputs the input with all characters composed.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni short &lt;input&gt;	Outputs basic info about the input, one character per line.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni rawshort &lt;input&gt;	Outputs basic info about the raw unprocessed input, one character per line.	
&amp;{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []}   uni help	Prints this help message.	

and here's roughly what I want

uni outputs Unicode information for characters.
USAGE:
uni &lt;input&gt;           Outputs Unicode info for the input.
Processes U+xxxx sequences into the appropriate characters and treats control characters as control.
uni raw &lt;input&gt;       Outputs Unicdoe info on the raw unporocessed input.
uni decomp &lt;input     Outputs the input with all characters decomposed.
uni comp &lt;input&gt;      Outputs the input with all characters composed.
uni short &lt;input&gt;     Outputs basic info about the input, one character per line.
uni rawshort &lt;input&gt;  Outputs basic info about the raw unprocessed input, one character per line.
uni help              Prints this help message.

I've tried all changing the minimum cell width to all sorts of values, from 0 to multiple hundreds, and it changes the alignment but not to the point of working correctly. I've tried tab vs space separators and Right Alignment vs default (left) alignment with no differences.

答案1

得分: 2

要写入 io.Writer,请使用 fmt.Fprintln(或 fmt.Fprintf)而不是 fmt.Println

// fmt.Println(writer, "uni outputs Unicode information for characters.")
fmt.Fprintln(writer, "uni outputs Unicode information for characters.")

fmt.Println 将尽力仅渲染每个参数 - 这就是为什么您看到了 tabwriter 的内部反射状态。

英文:

To write to an io.Writer use fmt.Fprintln (or fmt.Fprintf) instead of fmt.Println:

// fmt.Println(writer, &quot;uni outputs Unicode information for characters.&quot;)
fmt.Fprintln(writer, &quot;uni outputs Unicode information for characters.&quot;)

fmt.Println will do its best to just render each argument - hence why you are seeing the tabwriter's internal reflected state.

huangapple
  • 本文由 发表于 2022年2月18日 09:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/71167343.html
匿名

发表评论

匿名网友

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

确定