Go Take Input

www.i‮g‬iftidea.com

In Go, you can use the fmt package to read input from the user. Here's an example of how to use fmt.Scanln to read a string input from the user:

package main

import "fmt"

func main() {
    var name string
    fmt.Print("Enter your name: ")
    fmt.Scanln(&name)
    fmt.Printf("Hello, %s!\n", name)
}

In this example, we're declaring a variable name to store the user input, and then using fmt.Scanln to read a line of input from the user. The & operator is used to get the memory address of the name variable, which is passed as an argument to Scanln.

If you want to read a different type of input, such as an integer or a float, you can use the fmt.Scanf function instead. Here's an example:

package main

import "fmt"

func main() {
    var age int
    fmt.Print("Enter your age: ")
    fmt.Scanf("%d", &age)
    fmt.Printf("You are %d years old\n", age)
}

In this example, we're using the %d format specifier to read an integer input from the user. The value entered by the user is stored in the age variable, which is passed as an argument to Scanf.

You can also read multiple inputs in a single line using fmt.Scan or fmt.Scanf. Here's an example:

package main

import "fmt"

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

In this example, we're reading both the name and age input from the user using fmt.Scan. The & operator is used to get the memory addresses of the name and age variables, which are passed as arguments to Scan.