Skip to content

bufio: Reading from stdin breaks arrow keys #70071

@mybearworld

Description

@mybearworld

Go version

go version go1.23.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/mybearworld/.cache/go-build'
GOENV='/home/mybearworld/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/mybearworld/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/mybearworld/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/mybearworld/.go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/mybearworld/.go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/mybearworld/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/mybearworld/playground/stdinbufio/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3760656799=/tmp/go-build -gno-record-gcc-switches'

What did you do?

main.go:

package main

import (
        "bufio"
        "fmt"
        "os"
)

func main() {
        reader := bufio.NewReader(os.Stdin)
        s, err := reader.ReadString('\n')
        fmt.Println(s, err)
}

Keystrokes: Hello, world! and mars

What did you see happen?

Hello, world!^[[D and mars
Hello, world and mars
 <nil>

The arrow key is displayed as its code (^[[D) and deletes a character.

What did you expect to see?

Hello, world and mars!
Hello, world and mars!
 <nil>

The arrow key acts as it normally does (moving a character back and displaying it as such).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions