当我执行 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/