2019-06-25 09:37
浏览 130

编写完字符串后,golang io.writer新行

I have the following code that uses a package to draw a progress bar

type tmpStruct struct {

func (t *tmpStruct) Write(p []byte) (n int, err error) {
    fmt.Fprintf(os.Stdout, "%s", string(p))
    return len(p), nil

func demoLoadingBarCount(maximumInt int) {
    buf := tmpStruct{}
    if nBuf, ok := interface{}(&buf).(io.Writer); ok {
        bar := progressbar.NewOptions(
            progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
        for i := 0; i < maximumInt; i++ {
            time.Sleep(10 * time.Millisecond)

All works, except there is no new line at the end as you can see here enter image description here

I can't add a new line character in the Write function as that will cause it to new line after every byte pushed to the writer. Is there a neat way can I do this?

EDIT: I want the new line after the progress bar and before the next line prints out

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题