简体   繁体   中英

Clearing Buffer in Go

I'm new to Golang and while I'm trying to get consecutive input, The first scanf() gets the input and the remaining scanf() are omitted

Eg:

 fmt.Println("Enter A: ")
  fmt.Scanf("%d",a)
  fmt.Println("Enter B: ")
  fmt.Scanf("%d",b)

In this, the first Scanf works while the second one doesn't get any input

Use scan instead of scanf since you are trying to take int,

    var a, b int    
    fmt.Println("Enter A: ")
    fmt.Scan(&a)
    fmt.Println("Enter B: ")
    fmt.Scan(&b)

If you want a string input,

    reader := bufio.NewReader(os.Stdin)
    var a,b string
    fmt.Println("Enter A: ")
    a, _ := reader.ReadString("\n")
    fmt.Println("Enter B: ")
    b, _ := reader.ReadString("\n")

I think you made a typo:

fmt.Println("Enter B: ) 

fmt.Println("Enter B: ")

Notice the difference?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM