Golang: Input
Tags: go
Published: 2022-04-05 07:22:27

## Introduction

In this fourth section of Golang, we will be understanding the basics of user input. In golang we can get user input with several functions most of which are similar to the `C` programming language like `scanf`. This type of input is quite powerful and gives more control on the input to be received. 

## Scan Function

The [Scan](https://pkg.go.dev/fmt#Scan) function helps in getting a value with space as delimiter i.e. The input is stored before a space is encountered. This means the input is only limited to adding a space or a newline. We can use the function by passing the reference to the variable we are going to store the input value in. So, we can have a basic input in Golang as follows:

```go
package main

import "fmt"

func main() {
    var pname string
    fmt.Println("Enter your favourite programming language: ")
    fmt.Scan(&pname)
    fmt.Println("So, your favourite programming language is",pname)
}
```

```
$ go run scan.go
Enter your favourite programming language:
python
So, your favourite programming language is python
```

We need to declare the variable to take input as we need a reference of that variable to store the input. We will be talking about `&` and pointers in a separate article. We use the `Scan` function by passing the reference to the variable `pname` as `&pname` which means, fetch the memory address of the variable `pname`, we just pass the address as `int` to the Scan function and it does the rest to store the input value in it. We then as usual access the variable and operations on it. 

Here, if you add a space in the input, the value after the space won't be picked by the Scan function. It strictly stops accepting values input after it sees space. We can use this to input multiple varaibles at once. We know scan gets input before encountering space, so we can pass multiple variable references and add them as input.

```go
var (
    name   string
    age    int
    gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scan(&name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
```

```
$ go run scan.go
Enter your name age and gender:
Meet 19 77
Hello Meet, you are a M and 19 years old
```

Here, we are declaring multiple variables like `name`, `age`, and `gender` as `string`, `int`, and `rune` respectively. Then, we can input all of these in a single scan statement by comma separated variables. Here, we need to input the `rune` as a int value because under the hood it is a integer alias. So, we inputted `77` which is equivalent to `M` in ASCII characters and even Unicode character set. Thus, we were able to input multiple variables with the Scan function.

## Scanf functions

The [Scanf](https://pkg.go.dev/fmt#Scanf) function is quite similar to the `scanf` in C programming language as it allows to specify the type of the incoming input. This will solve the problem of us inputting `77` instead of `M` in the gender variable in the previous example. The Scanf function allows us to take input by specifying the placeholder types and the delimiters as well. Delimiter is basically the separator between two or more entities. Her we can either use space separation or `\n` as a input delimiter i.e. the way we want to separate inputs from each other while taking input.

```go
var (
    name   string
    age    int
    gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scanf("%s %d %c", &name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
```

```
$ go run scanf.go
Enter your name age and gender:
Meet 12 M
Hello Meet, you are a M and 12 years old


```

How cool is that? It definitely gives a much more control on what and how to take input. We are taking input as only space separated values. Let's now try to get more control over how the input will be taken and stored.

```go
var (
    name   string
    age    int
    gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scanf("%s \n %d %c", &name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
```

```
$ go run scanf.go
Enter your name age and gender:
Meet
12 M
Hello Meet, you are a M and 12 years old
```

By adding `\n` between the `%s`(name) and `%d`(age), we want the user to type the name on one line and age with gender on a different line. The age and gender as before separated by space. 


## Scanln functions

The [Scanln](https://pkg.go.dev/fmt#Scanln) function is an modification of the Scan function as it only stops the input after a newline/enter is pressed.  So, using this we can input multiple variables which are space separated in a single line. 

```go
var s string
fmt.Println("Enter a string: ")
fmt.Scanln(&s)
fmt.Println(s)
```

```
$ go run scanln.go
Enter a string:



$ go run scanln.go
Enter a string:
Can't type
Can't

$ ype
-bash: ype: command not found
```

The Scanln function even accepts empty string as input. It just needs to get the new line character and it will exit, it also only accepts space separated value.

The key difference in Scan and Scanln is that Scanln will not accept input which is space separated, Scan function considers the newline/enter as a space if there are multiple inputs. The below example will make things absolutely clear. 

```go
// scan.go
package main

import "fmt"

func main() {
        var (
                name   string
                age    int
                gender rune
        )
        fmt.Println("Enter your name age and gender: ")
        fmt.Scan(&name, &age, &gender)
        fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
}

```

```go
//scanln.go
package main

import "fmt"

func main() {
        var s string
        fmt.Println("Enter a string: ")
        fmt.Scanln(&s)
        fmt.Println("Inputted string : ", s)
}
```

```
$ go run scan.go
Enter your name age and gender:

Meet

14



77
Hello Meet, you are a M and 14 years old

$ go run scanln.go
Enter a string:

Inputted string :
```

We can see that, The Scan function won't exit until it has inputted all it's input values even with newline and spaces. Whereas the Scanln function just waits for the newline character (Enter Key) to be pressed and it exits, thereby even allowing empty string as input. 

That's it from this part. Reference for all the code examples and commands can be found in the [100 days of Golang](https://github.com/mr-destructive/100-days-of-golang/) GitHub repository.

## Conclusion

So, this are the basic input techniques in Golang. We saw functions in the `fmt` package like `Scan`, `Scanf`, and `Scanln` which allow us to get input in specific pattern. Hopefully, from this article part, we can build a firm base for further exploration like Strings, Arrays and the ways to input them. Thank you for reading. If you have any question or feedbacks, please let me know in the comments or on social handles. Happy Coding :)