0
0
Goprogramming~5 mins

Why input and output are required in Go

Choose your learning style9 modes available
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.

When you want to ask a user for their name and greet them.
When a program needs to read data from a file or keyboard.
When you want to show results or messages on the screen.
When your program needs to send data to another program or save it.
When you want to make your program interactive and useful.
Syntax
Go
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.

Examples
This reads a name from the user and then greets them.
Go
var name string
fmt.Scanln(&name)
fmt.Println("Hello,", name)
This reads an age number and prints it back with a message.
Go
var age int
fmt.Scanln(&age)
fmt.Println("You are", age, "years old")
Sample Program

This program asks the user to enter their name and then says hello using that name.

Go
package main

import "fmt"

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

Always use & before variable name in Scanln to pass its address.

Input and output make programs interactive and useful.

Summary

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.