Golang Fprintf, Fprint, and Fprintln | Go FMT Package

Golang fprint fprintf fprintln fmt package

In this blog, we are going to look at Golang Fprint, Fprintf, and the Fprintln Format functions from the Go FMT package.

Import Golang fmt package to use this Format function.

Golang Fprint

func Fprint(w io.Writer, a …interface{}) (n int, err error)

The Golang FMT package Fprint function takes a writer and variadic parameter in the form of empty interfaces and returns the number of bytes written as integer and an error with Golang error type.


fmt.Fprint(os.Stdout, "Golang Fprint")


Golang Fprint

Returning Values Example:

n, err := fmt.Fprint(os.Stdout, "Golang Fprint\n")
fmt.Fprint(os.Stdout, n, err)


Golang Fprint
14 <nil>

Golang Fprintf

func Fprintf(w io.Writer, format string, a …interface{}) (n int, err error)

The Golang Fprintf format function from the Go Fmt Package takes a writer function, a format String and empty interfaces as variadic parameters. The return values are same as the golang fprint function i.e the number of bytes written and the error.


s := "Golang Fprintf"
n, err := fmt.Fprintf(os.Stdout, "%v Type=%T\n", s, s)
fmt.Fprint(os.Stdout, n, err)


Golang Fprintf Type=string
27 <nil>

Golang Fprintln

func Fprintln(w io.Writer, a …interface{}) (n int, err error)

The Golang Fprintln Function is the same as the Golang Fprint function but it always adds a new line at the end and we do not have to do it manually.


n, err := fmt.Fprintln(os.Stdout, "Golang Fprint\n")
fmt.Fprint(os.Stdout, n, err)


Golang Fprint
14 <nil>

Writing to TCP Server Using Golang Fprintf

We know that the Go Fprint, Fprintf, and Fprintln functions always take the writer on which we have to perform the write function.

Earlier we used the standard out function from the Golang OS package.

This time we are going to use Golang TCP server to write on it.


package main

import (

func main() {
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
	defer listen.Close()
	for {
		conn, err := listen.Accept()
		if err != nil {
		fmt.Fprintln(conn, "Writing Using:")
		io.WriteString(conn, "WriteString function from the Golang IO Package\n")
		fmt.Fprint(conn, "Go Fprint\n")
		fmt.Fprintf(conn, "%v Type=%T", "Fprintf", "Fprintf")

Make sure that the Telnet feature is ON on Windows OS:

To Switch on the Telnet Feature on Windows, Go to Turn Windows Feature On or Off and Choose the Telnet and then apply the setting.

This will enable the telnet feature accessible using the command prompt in windows and the output will be shown.

Run the Go Code using Go Run or Build Command.

After Running the Go code, and run the telnet command on command prompt to open the TCP Server.

>  telnet localhost 8080


Writing Using:

WriteString function from the Golang IO Package

Go Fprint

Fprintf Type=string

The formatting may be different on the windows command prompt.

Go Fprint, Fprintf, and Fprintln functions are used to write on something that takes input from io.Writer.

Hope you like it!

Also, read Why Golang is called the future of Server-side language?

Learn more about Go Fprint, Fprintf, and Fprintln format functions from the Golang FMT package from the official Documentation.

Learn Golang Basics:

Learn Golang Advanced Topics:

Learn Golang Deeper:

Golang Packages:

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *