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(...)').
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.
So far, we have used variables to store values. But the variables were loaded with values.
package main import "fmt" func main() { var x = 9 fmt.Println("The value of x is : ",x) }
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.
package main import "fmt" func main() { var x int fmt.Print("Enter a number : ") fmt.Scanln(&x) fmt.Println("The number is :",x) }
Now, if you see the Output. The first line says,
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.
Then we have used the print statement,
So, what happened is, the below String is Printed,
Then we have the statement,
'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.
In the above case, I have typed '21'.
And '21' goes and sits inside 'x'.
And we got the output,
Next, let us look at Go Output.
A simple example of 'fmt.Print(...)' statement would be,
package main import "fmt" func main() { var x = 9 fmt.Println("The value of x is :",x) }
Also, we can print multiple values separated by ',' using the 'fmt.Print(...)' statement.
package main import "fmt" func main() { fmt.Println(5,6,9) }
With the 'fmt.Print(...)' statement, Go supports something called as 'Output Formatting'.
Let us dive into an example to understand 'Output Formatting'.
package main import "fmt" func main() { fmt.Printf("%s has a Son who studies in class %d", "John", 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.
The left part has the actual String to be printed, with two, so called placeholders %s and %d.
And the right side has the values to be placed in the above placeholder.
Now, if you look at the output,
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 |