- Go has three output functions:
- Print()
- Println()
- Printf()
1) Print(): function print its arguments with the default format.
- Program to illustrate print function.
package main import "fmt" func main() { var i, j string = "Prwa", "tech" fmt.Print(i) fmt.Print(j) }
Output :
- Program to illustrate print function in new line using “\n”.
package main import "fmt" func main() { var i, j string = "Data", "Science" fmt.Print(i, "\n") fmt.Print(j, "\n") }
Output :
- Program to illustrate print function in a line using “\n”.
package main import "fmt" func main() { var i, j string = "Data", "Science" fmt.Print(i, "\n", j) }
Output :
- Program to represent the print function of integer values.
package main import "fmt" func main() { var i, j = 10, 20 fmt.Print(i, j) }
Output :
2) println(): whitespace is added between the arguments, and a newline is added at the end:
package main import "fmt" func main() { var i, j string = "Parwa", "Tech" fmt.Println(i, j) }
Output :
3) Printf(): first formats its arguments based on the given formatting verb and then print them.
%v use to print the value of the arguments
%T used to print the type of the arguments
package main import "fmt" func main() { var i string = "PRWATECH" var j int = 10 fmt.Printf("i has value: %v and type: %T\n", i, i) fmt.Printf("j has value: %v and type: %T", j, j) }
Output:
- Formatting Verb for Printf()
Verb | Description
|
%v | Print the value in the default format |
%#v | Print the value in Go syntax format |
%T | Print the type of the value |
5. Program to illustrate printf function of verbs can be used with all data types.
package main import "fmt" func main() { var i = 10.5 var X = "PRWATECH" fmt.Printf("%v\n", i) fmt.Printf("%#v\n", i) fmt.Printf("%v%%\n", i) fmt.Printf("%T\n", i) fmt.Printf("%v\n", X) fmt.Printf("%#v\n", X) fmt.Printf("%T\n", X) }
Output :
6. Program to illustrate printf function of verbs can be used with integer data types.
package main import "fmt" func main() { var i = 10 fmt.Printf("%b\n ", i) fmt.Printf("%d\n ", i) fmt.Printf("%+d\n ", i) fmt.Printf("%o\n ", i) fmt.Printf("%O\n ", i) fmt.Printf("%x\n ", i) fmt.Printf("%X\n ", i) fmt.Printf("%#x\n ", i) fmt.Printf("%4d\n ", i) fmt.Printf("%-4d\n ", i) fmt.Printf("%04d\n ", i) }
Output :
The post Go output function appeared first on Prwatech.
This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here