fmt.Fprintln() 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.Fprintln() function in Go language formats using the default formats for its operands and writes to w. Here Spaces are always added between the specified operands and a newline is appended at the end. 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 Fprintln(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 and constant variables used in the code.

Return Value: It returns the number of bytes written and any write error encountered.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fprintln() function
  
// Including the main package
package main
  
// Importing fmt and os
import (
    "fmt"
    "os"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const name, dept = "GeeksforGeeks", "CS"
  
    // Calling Fprintln() function which returns
    // "n" as the number of bytes written and
    // "err" as any error ancountered
    n, err := fmt.Fprintln(os.Stdout, name, 
                   "is a", dept, "portal.")
  
    // Printing the number of bytes written
    fmt.Print(n, " bytes written.\n")
  
    // Printing if any error encountered
    fmt.Print(err)
  
}

chevron_right


Output:

GeeksforGeeks is a CS portal.
30 bytes written.
<nil>

In the above code, it can be seen that in Fprintln() function, no newline (\n) was added still it prints a new line which can be seen from the above-shown output.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// fmt.Fprintln() function
  
// Including the main package
package main
  
// Importing fmt and os
import (
    "fmt"
    "os"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const str1, str2, str3 = "a", "b", "c"
  
    // Calling Fprintln() function which returns
    // "n" as the number of bytes written and
    // "err" as any error ancountered
    n, err := fmt.Fprintln(os.Stdout, str1, str2, str3)
  
    // Printing the number of bytes written
    fmt.Print(n, " bytes written.\n")
  
    // Printing if any error encountered
    fmt.Print(err)
  
}

chevron_right


Output:

a b c
6 bytes written.
<nil>

In the above code, it can be seen that in Fprintln() function, no space was added still it prints including space which can be seen from the above-shown output.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.