fmt.Fprint() Function in Golang With Examples - GeeksforGeeks

05 May, 2020
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:
// Golang program to illustrate the usage of// fmt.Fprint() function  // Including the main packagepackage main  // Importing fmt and osimport (    "fmt"    "os")  // Calling mainfunc main() {      // Declaring some const variables    const name, dept = "GeeksforGeeks", "CS"      // Calling Fprint() function which returns    // "n" as the number of bytes written and     // "err" as any error ancountered    n, err := fmt.Fprint(os.Stdout, name, " is a ",                                dept, " portal.\n")      // Printing the number of bytes written    fmt.Print(n, " bytes written.\n")      // Printing if any error encountered    fmt.Print(err)  }
Output:
GeeksforGeeks is a CS portal.
30 bytes written.
<nil>

Example 2:
// Golang program to illustrate the usage of// fmt.Fprint() function  // Including the main packagepackage main  // Importing fmt and osimport (    "fmt"    "os")  // Calling mainfunc main() {      // Declaring some const variables    const num1, num2, num3 = "a", "b", "c"      // Calling Fprint() function which returns    // "n" as the number of bytes written and    // "err" as any error encountered    n, err := fmt.Fprint(os.Stdout, num1, num2, num3, "\n")      // Printing the number of bytes written    fmt.Print(n, " bytes written.\n")      // Printing if any error encountered    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:
// Golang program to illustrate the usage of// fmt.Fprint() function  // Including the main packagepackage main  // Importing fmt and osimport (    "fmt"    "os")  // Calling mainfunc main() {      // Declaring some const variables    const num1, num2, num3 = 5, 15, 15      // Calling Fprint() function which returns    // "n" as the number of bytes written and     // "err" as any error encountered    n, err := fmt.Fprint(os.Stdout, num1, num2, num3, "\n")      // Printing the number of bytes written    fmt.Print(n, " bytes written.\n")      // Printing if any error encountered    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.
Golang-fmt
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Parse JSON in Golang?
Defer Keyword in Golang
time.Parse() Function in Golang With Examples
Anonymous function in Go Language
Time Durations in Golang
Strings in Golang
How to convert a string in uppercase in Golang?
Loops in Go Language
Structures in Golang
Class and Object in Golang