# Go Cheatsheet - Get User Input from Stdin

## Get User Input from Stdin

### Scanln & Scanf

```go
var name string
fmt.Scanln(&name)  // this will scan only one string token (if there is a space in your string)

var age int
fmt.Scanf("%d\n", &age)
```

### bufio.NewScanner

```go
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
txt := sc.Text()  // read one line and save text to txt, it will contain spaces and exclude the ending \n
fmt.Println(txt)
```

### bufio.NewReader

```go
nr := bufio.NewReader(os.Stdin)
input, _ := nr.ReadString('\n')  // read till \n, the returned string will contain the trailing \n
input = strings.TrimRight(input, "\n")  // we remove the trailing \n
fmt.Println(input)
```
