Introduction
Input and output let a program talk with people or other programs. Input is how a program gets information, and output is how it shows results.
Input and output let a program talk with people or other programs. Input is how a program gets information, and output is how it shows results.
fmt.Scanln(&variable) // to get input from user fmt.Println(variable) // to show output to user
Use fmt.Scanln to read input from the keyboard.
Use fmt.Println to print output to the screen.
var name string
fmt.Scanln(&name)
fmt.Println("Hello,", name)var age int fmt.Scanln(&age) fmt.Println("You are", age, "years old")
This program asks the user to enter their name and then says hello using that name.
package main import "fmt" func main() { var name string fmt.Print("Enter your name: ") fmt.Scanln(&name) fmt.Println("Hello,", name) }
Always use & before variable name in Scanln to pass its address.
Input and output make programs interactive and useful.
Input lets programs get information from users or files.
Output lets programs show results or messages.
Without input and output, programs cannot interact with the world.
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
}package main
import "fmt"
func main() {
var age int
fmt.Scanln(age)
fmt.Println("Age is", age)
}Pass the test and this pattern moves unknown