C言語のgetchar()
の例
# include <stdio.h>
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c(%d)",ch, ch);
}
goでで実現する場合
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
fmt.Printf("Input Char Is : %v\n", string([]byte(input)[0]))
// fmt.Printf("You entered: %v\n", []byte(input))
// fmt.Println(input)
}
golangの場合は、Enter
が押された時、最後に改行文字\n
(U+0010)を表示しますが、C言語のgetchar()
では場合は検知することができません
c言語でこれを検知する場合には、getch()
を使って、検知をして下さい
参考文献
- https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/zhBE5MH4n-Q
- https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/S9AO_kHktiY
- https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/icMfYF8wJCk
- http://stackoverflow.com/questions/14094190/golang-function-similar-to-getchar