Go Fmt: Fprintf, Sprintf, Scanf, and Sscan Read it later

5/5 - (1 vote)

The fmt package is one of the essential packages in the Go language, which provides functionality for formatting I/O streams. The package contains several functions that allow us to format data, print it to the console or file, and read data from the user. In this blog, we will explore the different functions available in the fmt package, including Fprintf, Fprint, Fprintln, Sprintf, Scanf, and Sscan.

Go fmt Fprintf

The Fprintf function is used to format and print data to a file or an output stream. Its syntax is as follows:

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

The function takes an io.Writer interface, a format string, and a variadic argument list of values. The values are passed in the same order as they appear in the format string. The Fprintf function formats the data according to the format string and writes it to the specified io.Writer.

Here’s an example of how to use the Fprintf function:

package main

import (
	"fmt"
	"os"
)

func main() {
	file, err := os.Create("output.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	name := "John"
	age := 30

	fmt.Fprintf(file, "Name: %s, Age: %d\n", name, age)
}

In this example, we create a new file named output.txt and write the formatted string to it using the Fprintf function. The output of this program will be a file with the following contents:

Name: John, Age: 30

Go fmt Fprint

The Fprint function is similar to Fprintf but writes the formatted string to a writer instead of a file. Its syntax is as follows:

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

The function takes an io.Writer interface and a variadic argument list of values. The values are printed to the specified io.Writer in the default format.

Here’s an example of how to use the Fprint function:

package main

import (
	"fmt"
	"os"
)

func main() {
	name := "John"
	age := 30

	fmt.Fprint(os.Stdout, "Name: ", name, ", Age: ", age, "\n")
}

In this example, we print the formatted string to the console using the Fprint function. The output of this program will be:

Name: John, Age: 30

Go Sprintf

The Sprintf function is used to

format and return a formatted string. Its syntax is as follows:

func Sprintf(format string, a ...interface{}) string

The function takes a format string and a variadic argument list of values. The values are passed in the same order as they appear in the format string. The Sprintf function formats the data according to the format string and returns a formatted string.

Here’s an example of how to use the Sprintf function:

package main

import (
	"fmt"
)

func main() {
	name := "John"
	age := 30

	output := fmt.Sprintf("Name: %s, Age: %d\n", name, age)
	fmt.Print(output)
}

In this example, we format the string using the Sprintf function and print it to the console using the Print function. The output of this program will be:

Name: John, Age: 30

Go Scanf

The Scanf function is used to read formatted data from standard input (os.Stdin). Its syntax is as follows:

func Scanf(format string, a ...interface{}) (n int, err error)

The function takes a format string and a variadic argument list of values. The values are pointers to the variables where the scanned data will be stored. The Scanf function reads data from standard input and stores it in the specified variables according to the format string.

Here’s an example of how to use the Scanf function:

package main

import (
	"fmt"
)

func main() {
	var name string
	var age int

	fmt.Print("Enter your name: ")
	fmt.Scanf("%s", &name)

	fmt.Print("Enter your age: ")
	fmt.Scanf("%d", &age)

	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

In this example, we read the name and age from standard input using the Scanf function and print it to the console using the Printf function. The output of this program will be:

Enter your name: John
Enter your age: 30
Name: John, Age: 30

Golang fmt Sscan

The Sscan function is similar to Scanf but reads data from a string instead of standard input. Its syntax is as follows:

func Sscan(str string, a ...interface{}) (n int, err error)

The function takes a string and a variadic argument list of values. The values are pointers to the variables where the scanned data will be stored. The Sscan function reads data from the specified string and stores it in the specified variables according to the format string.

Here’s an example of how to use the Sscan function:

package main

import (
	"fmt"
)

func main() {
	var name string
	var age int

	input := "John 30"
	fmt.Sscan(input, &name, &age)

	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

In this example, we read the name and age from a string using the Sscan function and print it to the console using the Printf function. The output of this program will be:

Name: John, Age: 30

Reference:

Was This Article Helpful?

Leave a Reply

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