繁体   English   中英

golang tabwriter 打印垃圾且格式不正确

[英]golang tabwriter printing junk and not formatting properly

我正在尝试使用 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()
}

然而,output 都包含大量不应打印的内部垃圾,并且未正确对齐列。

这是我得到的:

&{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 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.

我已经尝试将最小单元格宽度更改为各种值,从 0 到数百,它更改了 alignment 但没有达到正常工作的程度。 我试过制表符与空格分隔符和右 Alignment 与默认(左)alignment 没有区别。

要写入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 的内部反射 state。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM