fmt.Fprint() Function in Golang With Examples
In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Fprint() function in Go language formats using the default formats for its operands and writes to w. 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 Fprint(w io.Writer, a ...interface{}) (n int, err error)
Parameters: This function accepts two parameters which are illustrated below:
- w io.Writer: This is the specified standard input or output.
- a …interface{}: This is containing some strings or constant variables used in the code.
Return Value: It returns the number of bytes written and any write error encountered.
Example 1:
package main
import (
"fmt"
"os"
)
func main() {
const name, dept = "GeeksforGeeks" , "CS"
n, err := fmt.Fprint(os.Stdout, name, " is a " ,
dept, " portal.\n" )
fmt.Print(n, " bytes written.\n" )
fmt.Print(err)
}
|
Output:
GeeksforGeeks is a CS portal.
30 bytes written.
<nil>
Example 2:
package main
import (
"fmt"
"os"
)
func main() {
const num1, num2, num3 = "a" , "b" , "c"
n, err := fmt.Fprint(os.Stdout, num1, num2, num3, "\n" )
fmt.Print(n, " bytes written.\n" )
fmt.Print(err)
}
|
Output:
abc
4 bytes written.
<nil>
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"
"os"
)
func main() {
const num1, num2, num3 = 5, 15, 15
n, err := fmt.Fprint(os.Stdout, num1, num2, num3, "\n" )
fmt.Print(n, " bytes written.\n" )
fmt.Print(err)
}
|
Output:
5 15 15
8 bytes written.
<nil>
In the above code, the constant variables used are numbers hence spaces are added in between two numbers shown above in the output.
Last Updated :
05 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...