Go Input

Reading user input and data in Go programs

📥 What is Go Input?

Go input allows you to read data from users through the console. The fmt package provides Scan functions to capture user input and store it in variables for processing.


package main

import "fmt"

func main() {
    var name string
    fmt.Print("Enter your name: ")
    fmt.Scan(&name)
    fmt.Println("Hello", name)
}
                                    

Output:

Enter your name: Alice
Hello Alice

Go Input Functions

⌨️

fmt.Scan()

Reads input separated by spaces

var name string
fmt.Scan(&name)
📝

fmt.Scanln()

Reads input until newline

var age int
fmt.Scanln(&age)
🎯

fmt.Scanf()

Formatted input with patterns

var name string
fmt.Scanf("%s", &name)
🔢

Multiple Inputs

Read multiple values at once

var x, y int
fmt.Scan(&x, &y)

🔹 Basic Input Example

Simple program to get user input:

package main

import "fmt"

func main() {
    var name string
    var age int
    
    fmt.Print("Enter your name: ")
    fmt.Scan(&name)
    
    fmt.Print("Enter your age: ")
    fmt.Scan(&age)
    
    fmt.Printf("Hello %s, you are %d years old!\n", name, age)
}

Output:

Enter your name: Bob
Enter your age: 25
Hello Bob, you are 25 years old!

🔹 Multiple Input Values

Reading multiple values in one line:

package main

import "fmt"

func main() {
    var x, y, z int
    
    fmt.Print("Enter three numbers: ")
    fmt.Scan(&x, &y, &z)
    
    sum := x + y + z
    fmt.Printf("Numbers: %d, %d, %d\n", x, y, z)
    fmt.Printf("Sum: %d\n", sum)
}

Output:

Enter three numbers: 10 20 30
Numbers: 10, 20, 30
Sum: 60

🔹 Different Data Types Input

Reading various data types:

package main

import "fmt"

func main() {
    var name string
    var age int
    var height float64
    var isStudent bool
    
    fmt.Print("Enter name: ")
    fmt.Scan(&name)
    
    fmt.Print("Enter age: ")
    fmt.Scan(&age)
    
    fmt.Print("Enter height: ")
    fmt.Scan(&height)
    
    fmt.Print("Are you a student? (true/false): ")
    fmt.Scan(&isStudent)
    
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Age: %d\n", age)
    fmt.Printf("Height: %.1f\n", height)
    fmt.Printf("Student: %t\n", isStudent)
}

Output:

Enter name: Alice
Enter age: 22
Enter height: 5.5
Are you a student? (true/false): true
Name: Alice
Age: 22
Height: 5.5
Student: true

🔹 Interactive Calculator

Simple calculator using input:

package main

import "fmt"

func main() {
    var num1, num2 float64
    var operator string
    
    fmt.Print("Enter first number: ")
    fmt.Scan(&num1)
    
    fmt.Print("Enter operator (+, -, *, /): ")
    fmt.Scan(&operator)
    
    fmt.Print("Enter second number: ")
    fmt.Scan(&num2)
    
    switch operator {
    case "+":
        fmt.Printf("Result: %.2f\n", num1+num2)
    case "-":
        fmt.Printf("Result: %.2f\n", num1-num2)
    case "*":
        fmt.Printf("Result: %.2f\n", num1*num2)
    case "/":
        fmt.Printf("Result: %.2f\n", num1/num2)
    default:
        fmt.Println("Invalid operator")
    }
}

Output:

Enter first number: 15
Enter operator (+, -, *, /): *
Enter second number: 3
Result: 45.00

🧠 Test Your Knowledge

What symbol is used before a variable in Scan functions?