一种scanf
var x int
fmt.Println("input a int number")
fmt.Scan(&x)
fmt.Printf("读取到内容:%d he",x)
一种 bufio
reader := bufio.NewReader(os.Stdin)
res,_ := reader.ReadString(' ')
fmt.Printf("读取到内容%s ",res)
package main import ( "bufio" "fmt" "os" ) func main() { var x int fmt.Println("input a int number") fmt.Scan(&x) fmt.Printf("读取到内容:%d he",x) reader := bufio.NewReader(os.Stdin) res,_ := reader.ReadString(' ') fmt.Printf("读取到内容%s ",res) }
执行结果
GOROOT=/usr/local/go #gosetup GOPATH=/www/gopath #gosetup /usr/local/go/bin/go build -o /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_test_test test/test #gosetup /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_test_test input a int number 3 读取到内容:3 hello 读取到内容hello