Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: Scan extra input when switching languages #45354

Closed
Dezmmond opened this issue Apr 2, 2021 · 4 comments
Closed

fmt: Scan extra input when switching languages #45354

Dezmmond opened this issue Apr 2, 2021 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@Dezmmond
Copy link

Dezmmond commented Apr 2, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2160470449=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I wrote and tested this program:

My program Output
package main

import . "fmt"

func main() {
var (
name string
age int
)
Print("Введите имя: ")
Scanln(&name)
Print("Введите возраст: ")
Scanln(&age)

Println(name, age)

}

Algorithm:

  1. Started the program.
  2. In the name input field, I entered the text first in Cyrillic (text: Vuy), then completely erased it, switched the layout from American to Russian with the keyboard shortcut "Alt+Shift" and wrote in Latin (text: Dezm), then pressed the "Enter"button.
  3. In the age input field, enter the numbers (88), and then click the "Enter" button.

What did you expect to see?

In stdout i expected to see: Dezm 88

What did you see instead?

But i see instead: ВуDezm 88

@seankhliao seankhliao changed the title Error in the fmt.Scan() and fmt.Scanln() functions fmt: Scan extra input when switching languages Apr 2, 2021
@seankhliao
Copy link
Member

Can you try it with the following and post the output?
Though I think this is more likely an issue with the terminal and/or input method than with Go.

(cribbed from #23562 (comment))

package main

import (
	"fmt"
	"os"
)

type myReader struct{}

func (r myReader) Read(p []byte) (n int, err error) {
	n, err = os.Stdin.Read(p)
	fmt.Fprintf(os.Stderr, "DEBUG: %q %v\n", p[:n], err)
	return n, err
}

func main() {
	var (
		name string
		age  int

		input = &myReader{}
	)

	fmt.Print("Введите имя: ")
	fmt.Fscanln(input, &name)

	fmt.Print("Введите возраст: ")
	fmt.Fscanln(input, &age)

	fmt.Println(name, age)
}

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 2, 2021
@ghost
Copy link

ghost commented Apr 3, 2021

I can't reproduce this in Konsole, so I guess the culprit is indeed the issue author's terminal emulator or input method.

@Dezmmond
Copy link
Author

Dezmmond commented Apr 5, 2021

This is my output:

Введите имя: Dezm
DEBUG: "\xd0"
DEBUG: "\x92"
DEBUG: "\xd1"
DEBUG: "\x83"
DEBUG: "D"
DEBUG: "e"
DEBUG: "z"
DEBUG: "m"
DEBUG: "\n"
Введите возраст: 88
DEBUG: "8"
DEBUG: "8"
DEBUG: "\n"
ВуDezm 88

@seankhliao
Copy link
Member

So fmt is correctly processing what comes in on STDIN, you'll have to look elsewhere to find why the extra characters are passed to the program.

Closing as this is not a bug with Go

@golang golang locked and limited conversation to collaborators Apr 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants