亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang io.writer 寫完字符串后換行

golang io.writer 寫完字符串后換行

Go
米脂 2023-06-26 17:16:18
我有以下代碼,使用包來繪制進度條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(? ? ? ? ? ? maximumInt,? ? ? ? ? ? progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),? ? ? ? ? ? progressbar.OptionSetWidth(100),? ? ? ? ? ? progressbar.OptionSetWriter(nBuf),? ? ? ? )? ? ? ? for i := 0; i < maximumInt; i++ {? ? ? ? ? ? bar.Add(1)? ? ? ? ? ? time.Sleep(10 * time.Millisecond)? ? ? ? }? ? }}一切正常,除了最后沒有換行,正如您在此處看到的那樣?我無法在 Write 函數中添加新行字符,因為這會導致在將每個字節推送到寫入器后將其添加到新行。有什么巧妙的方法可以做到這一點嗎?編輯:我想要在進度條之后和下一行打印之前添加新行
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

您所問問題的簡單答案就是在進度條完成后打印一個額外的換行符:


func demoLoadingBarCount(maximumInt int) {

? ? buf := &tmpStruct{}

? ? bar := progressbar.NewOptions(

? ? ? ? maximumInt,

? ? ? ? progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),

? ? ? ? progressbar.OptionSetWidth(100),

? ? ? ? progressbar.OptionSetWriter(buf),

? ? )

? ? for i := 0; i < maximumInt; i++ {

? ? ? ? bar.Add(1)

? ? ? ? time.Sleep(10 * time.Millisecond)

? ? }

? ? fmt.Fprintf(buf, "\n") // <---- Add this

}

查看完整回答
反對 回復 2023-06-26
  • 1 回答
  • 0 關注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號