unix - Go fmt 会自动缩进吗?

标签 unix go terminal

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

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

这是我的代码:

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)
    }
}

最佳答案

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


例如,

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)
    }
}

输出:

$ go run terminal.go
13
13
13
13
13
113 ('q')
$ 

关于unix - Go fmt 会自动缩进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841662/

相关文章:

Golang 保持活力

macos - 如何让 ZSH 在终端框架中显示当前目录?

json - 将 API 请求函数传递给另一个函数 GoLang

go - channel 数组的范围

linux - 字体的终端转义序列

mysql - 按照 Sharetribe 安装说明进行操作并收到错误 "ERROR 1064 (42000): You have an error in your SQL syntax;"

unix - 如何从 grep 中保存匹配和不匹配的内容

matlab - 从 Unix shell 读取 .mat 文件

java - 使用 java.lang.Process 和特定用户从 Java 类执行 unix 程序

apache - 如何在可以通过 ssh 连接的服务器上打开 Web 浏览器?