Home >Backend Development >Golang >How to Get Single Character Input in Go Without Pressing Enter?
Obtaining Character Input Without Pressing Enter in Go
In order to avoid pressing the Enter key after receiving character input in Go, you can utilize the following approach.
Example
The following code illustrates how to implement this approach:
package main import ( "fmt" "os" "os/exec" ) func main() { // disable input buffering exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() // do not display entered characters on the screen exec.Command("stty", "-F", "/dev/tty", "-echo").Run() var b []byte = make([]byte, 1) for { os.Stdin.Read(b) fmt.Println("I got the byte", b, "("+string(b)+")") } }
This solution provides a similar functionality to Console.ReadKey() in C# by allowing you to read a single character without waiting for the user to press Enter.
The above is the detailed content of How to Get Single Character Input in Go Without Pressing Enter?. For more information, please follow other related articles on the PHP Chinese website!