Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - INPUT & OUTPUT


Input and Output are preferably the most important statements in a programming language.


So far, we have seen the Output command of Go(i.e. 'fmt.Println(...)').


Example :



package main
import "fmt"
    
func main() {
    
    fmt.Println("Hello World") 
}	 


And we are yet to look at the Input statement.


So, let us look at the Input statement. Then we will be exploring a few more ways to use the Output statement.


Go Input


So far, we have used variables to store values. But the variables were loaded with values.


Example :



package main
import "fmt"
    
func main() {
    
    var x = 9
    fmt.Println("The value of x is : ",x)
} 


Output :



 The value of x is : 9

But, what if you don't want the variable 'x' to contain a fixed value like '9'. Rather you want the user to enter any value of their choice.


And this is where the Input statements comes to picture.


Just like the Output statement is called 'fmt.Println()'. Same way the Input statement is called as 'input()'.


Let us understand 'input()' statement with the below example.


Example :



package main
import "fmt"
    
func main() {
    
    var x int
    fmt.Print("Enter a number : ")
    fmt.Scanln(&x)
    fmt.Println("The number is :",x)
}


Output :



 Enter a number : 21
 The number is : 21

Now, if you see the Output. The first line says,


 Enter any number of your choice : 21

Now, let us go to the above code and see what happened?


In the above code, we have declared an integer type variable, which is initially empty.


 var x int

java_Collections

Then we have used the print statement,


 fmt.Print("Enter a number : ")

So, what happened is, the below String is Printed,


 Enter a number :

Then we have the statement,


 fmt.Scanln(&x)

'fmt.Scanln(&x)' is used to get an input from the user, and if the user enters a value, it goes and sits inside variable 'x'.


So, the screen is stuck, until user types any number.


java_Collections

In the above case, I have typed '21'.


 Enter a number : 21

And '21' goes and sits inside 'x'.


java_Collections

And we got the output,


 The number is : 5

Next, let us look at Go Output.


Go Output


A simple example of 'fmt.Print(...)' statement would be,


Example :



package main
import "fmt"
    
func main() {
    
    var x = 9
    fmt.Println("The value of x is :",x)
}	


Output :



 The value of x is : 9

Also, we can print multiple values separated by ',' using the 'fmt.Print(...)' statement.


Example :



package main
import "fmt"
    
func main() {
    
    fmt.Println(5,6,9)
}	


Output :



 5  6  9

Formatting Output using '%'


With the 'fmt.Print(...)' statement, Go supports something called as 'Output Formatting'.


Let us dive into an example to understand 'Output Formatting'.


Example :



package main
import "fmt"
    
func main() {
    
    fmt.Printf("%s has a Son who studies in class %d", "John", 4)
}


Output :



 John has a Son who studies in class 4

Now, in the above code we have replaced 'fmt.Println(...)' with 'fmt.Printf(...)' to support formatting.


So, inside the 'fmt.Printf(...)' statement, there are two parts.


 fmt.Printf("%s has a Son who studies in class %d", "John", 4)

The left part has the actual String to be printed, with two, so called placeholders %s and %d.


 '%s has a Son who studies in class %d'

And the right side has the values to be placed in the above placeholder.


 "John", 4

Now, if you look at the output,


 John has a Son who studies in class 4

You can see that '%s' is replaced with 'John' and '%d' is replaced with '4'.


Similarly, we can use other placeholders for all types of data.


Below is the Array :


Type Description
d Decimal Number
x, X Hexadecimal Number
o Octal Number
f, F Floating Point Number
e, E Exponential Number
g, G Floating point/Exponential
c Single character
s String