function similar to getchar

C’s getchar() example: #include <stdio.h> void main() { char ch; ch = getchar(); printf(“Input Char Is :%c”,ch); } Go equivalent: package main import ( “bufio” “fmt” “os” ) func main() { reader := bufio.NewReader(os.Stdin) input, _ := reader.ReadString(‘\n’) fmt.Printf(“Input Char Is : %v”, string([]byte(input)[0])) // fmt.Printf(“You entered: %v”, []byte(input)) } The last commented line just … Read more

What is the equivalent to getch() & getche() in Linux?

#include <termios.h> #include <stdio.h> static struct termios old, current; /* Initialize new terminal i/o settings */ void initTermios(int echo) { tcgetattr(0, &old); /* grab old terminal i/o settings */ current = old; /* make new settings same as old settings */ current.c_lflag &= ~ICANON; /* disable buffered i/o */ if (echo) { current.c_lflag |= ECHO; … Read more

How to avoid pressing Enter with getchar() for reading a single character only?

This depends on your OS, if you are in a UNIX like environment the ICANON flag is enabled by default, so input is buffered until the next ‘\n’ or EOF. By disabling the canonical mode you will get the characters immediately. This is also possible on other platforms, but there is no straight forward cross-platform … Read more