Go fmt会自动缩进吗?

Qou*_*roy 2 unix terminal go

当我这样做fmt.Printf("...\n")时不会将光标移动到第0列,因此下一行是缩进的:

13
  13
    13
      13
        13
          13
            113 ('q')
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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\n", c)
        } else {
            fmt.Printf("%d ('%c')\n", c, c)
        }
    }

    if err != nil {
        panic(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

pet*_*rSO 6

评论:您将终端设置为原始模式,是否需要回车才能将光标放在行的开头? - JimB


例如,

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\r\n", c)
        } else {
            fmt.Printf("%d ('%c')\r\n", c, c)
        }
    }

    if err != nil {
        panic(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ go run terminal.go
13
13
13
13
13
113 ('q')
$ 
Run Code Online (Sandbox Code Playgroud)