英文:
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()
}
然而,输出结果包含了一些不应该打印的内部垃圾信息,并且列没有正确对齐。
这是我得到的结果:
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} uni outputs Unicode information for characters.
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} USAGE:
&{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.
&{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.
&{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.
&{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.
&{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.
&{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.
&{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.
&{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, ' ', 0)
fmt.Println(writer, "uni outputs Unicode information for characters.")
fmt.Println(writer, "USAGE:")
fmt.Println(writer, " uni <input>\tOutputs Unicode info for the input.\t")
fmt.Println(writer, "\tProcesses U+xxxx sequences into the appropriate characters and treats control characters as control.\t")
fmt.Println(writer, " uni raw <input>\tOutputs Unicode info on the raw unporocessed input.\t")
fmt.Println(writer, " uni decomp <input\tOutputs the input with all characters decomposed.\t")
fmt.Println(writer, " uni comp <input>\tOutputs the input with all characters composed.\t")
fmt.Println(writer, " uni short <input>\tOutputs basic info about the input, one character per line.\t")
fmt.Println(writer, " uni rawshort <input>\tOutputs basic info about the raw unprocessed input, one character per line.\t")
fmt.Println(writer, " uni help\tPrints this help message.\t")
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:
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} uni outputs Unicode information for characters.
&{0xc000006018 100 8 1 [32 32 32 32 32 32 32 32] 0 [] 0 {0 0 false} 0 [[]] []} USAGE:
&{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.
&{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.
&{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.
&{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.
&{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.
&{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.
&{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.
&{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 <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 Unicdoe 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.
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, "uni outputs Unicode information for characters.")
fmt.Fprintln(writer, "uni outputs Unicode information for characters.")
fmt.Println
will do its best to just render each argument - hence why you are seeing the tabwriter's internal reflected state.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论