Skip to content

fmt: Scanf rejects \r\n at end of line on Windows #5391

@gopherbot

Description

@gopherbot

by xuxinhua1984:

The code:

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 2; i++ {
        var name string
        fmt.Print("Input Name:")
        n, err := fmt.Scanf("%s", &name)
        fmt.Println(n, err, name)
    }   
}

Build And Run.

Then, I input:golang,and enter,the result is:

Input Name:golang
1 <nil> golang
Input Name:0 unexpected newline

But on linux,the same code, the result is ok:

Input Name:golang
1 <nil> golang
Input Name:golang
1 <nil> golang

The reason is: on windows, The "enter" is "\r\n", but in fmt
package, it is only deal "\n".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions