dongwuqi4243 2018-12-18 21:54
浏览 153
已采纳

Go fmt会自动缩进吗?

When I do fmt.Printf("... ") it doesn't move the cursor to the 0th column and the next line is therefore indented:

13
  13
    13
      13
        13
          13
            113 ('q')

Here is my code:

package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    oldState, err := terminal.MakeRaw(0)
    if err != nil {
        panic(err)
    }
    defer terminal.Restore(0, oldState)

    reader := bufio.NewReader(os.Stdin)

    var c rune
    for err == nil {
        if c == 'q' {
            break
        }

        c, _, err = reader.ReadRune()

        if unicode.IsControl(c) {
            fmt.Printf("%d
", c)
        } else {
            fmt.Printf("%d ('%c')
", c, c)
        }
    }

    if err != nil {
        panic(err)
    }
}
  • 写回答

1条回答 默认 最新

  • douyi6960 2018-12-18 22:22
    关注

    Comment: You're putting the terminal in raw mode, doesn't that require a carriage return to put the cursor at the start of the line? – JimB


    For example,

    terminal.go:

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "unicode"
    
        "golang.org/x/crypto/ssh/terminal"
    )
    
    func main() {
        oldState, err := terminal.MakeRaw(0)
        if err != nil {
            panic(err)
        }
        defer terminal.Restore(0, oldState)
    
        reader := bufio.NewReader(os.Stdin)
    
        var c rune
        for err == nil {
            if c == 'q' {
                break
            }
    
            c, _, err = reader.ReadRune()
    
            if unicode.IsControl(c) {
                fmt.Printf("%d
    ", c)
            } else {
                fmt.Printf("%d ('%c')
    ", c, c)
            }
        }
    
        if err != nil {
            panic(err)
        }
    }
    

    Output:

    $ go run terminal.go
    13
    13
    13
    13
    13
    113 ('q')
    $ 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题