fmt.print() Function in Golang With Examples
Last Updated :
05 May, 2020
In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.print() function in Go language formats using the default formats for its operands and writes to standard output. Here Spaces are added between operands when any string is not used as a parameter. Moreover, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.
Syntax:
func Print(a ...interface{}) (n int, err error)
Here, “a …interface{}” containing some strings and declared constant variables.
Return Value: It returns the number of bytes written and any write error encountered.
Example 1:
package main
import (
"fmt"
)
func main() {
const name, dept = "GeeksforGeeks" , "CS"
fmt.Print(name, " is a " , dept, " portal.\n" )
}
|
Output:
GeeksforGeeks is a CS portal.
Example 2:
package main
import (
"fmt"
)
func main() {
const str1, str2, str3 = "a" , "b" , "c"
fmt.Print(str1, str2, str3, "\n" )
}
|
Output:
abc
In the above code, the constant variables used are strings hence spaces are not added in between two strings shown above in the output.
Example 3:
package main
import (
"fmt"
)
func main() {
const num1, num2, num3 = 5, 15, 15
fmt.Print(num1, num2, num3, "\n" )
}
|
Output:
5 15 15
In the above code, the constant variables used are numbers hence spaces are added in between two numbers shown above in the output.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...