Golang fmt Package

Golang | fmt Package: Here, we are going to learn about the Golang fmt package, its format verbs and functions with examples.

In Go language, the fmt package implements formatted I/O with functions analogous to C's printf() and scanf(). It contains a various variety of functions for formatting. The format 'verbs' are derived from C's but are simpler.

The fmt stands for the Format package. This package allows for strings formatting, values formatting, or anything. It also allows us to format errors, so that formatted errors can be used when required if anything goes wrong in the code.

To use these fmt package verbs and functions – we need to import the fmt package.

fmt Package import statement:

import "fmt"

List of Golang fmt Package Functions

The fmt package has the following functions,

Functions

Function Description
fmt.Errorf() It formats according to a format specifier and returns the string as a value that satisfies error, i.e., the Errorf() function allows us to use formatting features to create descriptive error messages.
fmt.Fprint() It formats using the default formats for its operands and writes to w (io.Writer). If there is no string then spaces are added between operands and returns the number of total bytes written and an error if occurred during the write operation.
fmt.Fprintf() It formats according to the given format specifiers and writes to w (io.Writer), and returns the number of total bytes written and an error if occurred during the write operation.
fmt.Fprintln() It formats using the default formats for its operands and writes to w (io.Writer). The spaces are always added between operands and a newline is appended and returns the number of total bytes written and an error if occurred during the write operation.
fmt.Fscan() It scans the text read from r (io.Reader), and stores the successive space-separated values into the given successive arguments. The newlines count as space and return the number of total items successfully scanned and an error if occurred during the read operation.
fmt.Fscanf() It scans the text read from r (io.Reader), and stores the successive space-separated values into the given successive arguments as determined by the format specifiers (format verbs). The newlines count as space and return the number of total items successfully scanned and an error if occurred during the read operation.
fmt.Fscanln() It is similar to Fscan() function, but it stops scanning at a newline and after the final item there must be a newline or EOF and returns the number of total items successfully scanned and an error if occurred during the read operation.
fmt.Print() formats using the default formats for its operands and writes to standard output. If there is no string, then the spaces are added between operands and return the number of total bytes written and an error if occurred during the write operation.
fmt.Printf() formats according to a format specifier and writes to standard output. It accepts two parameters (format string, a ...interface{}) and returns the number of total bytes written and an error if occurred during the write operation.
fmt.Println() It formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It accepts one parameter (a ...interface{}) and returns the number of total bytes written and an error if occurred during the write operation.
fmt.Scan() scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It accepts one parameter (a ...interface{}) and returns the number of total items successfully scanned and an error if occurred during the read operation.
fmt.Scanf() It scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It accepts two parameters (format string, a ...interface{}) and returns the number of total items successfully Scanfned and an error if occurred during the read operation.
fmt.Scanln() Scanln() function is similar to Scan() function, but it stops the scanning at a newline and after the final item there must be a newline or EOF. It accepts one parameter (a ...interface{}) and returns the number of total items successfully scanned and an error if occurred during the read operation.
fmt.Sprint() It formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string. It accepts one parameter (a ...interface{}) and returns the formatted string.
fmt.Sprintf() It formats according to a format specifier and returns the resulting string. It accepts two parameters (format string, a ...interface{}) and returns the formatted string.
fmt.Sprintln() It formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended. It accepts one parameter (a ...interface{}) and returns the formatted string.
fmt.Sscan() scans the argument string, storing successive space-separated values into successive arguments. Newlines count as space. It accepts two parameters (str string, a ...interface{}) and returns the number of total items successfully parsed and an error if occurred during paring.
fmt.Sscanf() It scans the argument string, storing successive space-separated values into successive arguments as determined by the format. It accepts three parameters (str string, format string, a ...interface{}) and returns the number of total items successfully parsed and an error if occurred during paring.
fmt.Sscanln() It Sscanln() function is similar to Sscan() function but stops scanning at a newline and after the final item, there must be a newline or EOF. It accepts two parameters (str string, a ...interface{}) and returns the number of total items successfully parsed and an error if occurred during paring.

Example of fmt.Printf() function

// Golang program to demonstrate the
// example of fmt.Printf() function

package main

import (
	"fmt"
)

func main() {
	// Print text with new line
	fmt.Printf("Hello World\n")
	fmt.Printf("Hi, there...\n")

	// Printing text, values together
	fmt.Printf("Name: %s, Age: %d\n", "Alex", 21)

	// Printing variable values

	// Declaring & assigning variables
	var (
		name string
		age  int
		perc float32
	)

	name = "Alex"
	age = 21
	perc = 87.5

	// Printing
	fmt.Printf("Name: %s, Age: %d, Perc: %.2f\n", name, age, perc)
}

Output:

Hello World
Hi, there...
Name: Alex, Age: 21
Name: Alex, Age: 21, Perc: 87.50

Example of fmt.Scanf() function

// Golang program to demonstrate the
// example of fmt.Scanf() function
// Input two numbers & find their sum

package main

import (
	"fmt"
)

func main() {
	var num1 int
	var num2 int
	var sum int

	// Input numbers
	fmt.Print("Enter first number: ")
	fmt.Scanf("%d", &num1)

	fmt.Print("Enter second number: ")
	fmt.Scanf("%d", &num2)

	// Calculate sum
	sum = num1 + num2

	// Printing the values & sum
	fmt.Printf("%d + %d = %d\n", num1, num2, sum)
}

Output:

Enter first number: 108
Enter second number: 101
108 + 101 = 209

Example of fmt.Sprintf() function

// Golang program to demonstrate the
// example of fmt.Sprintf() function

package main

import (
	"fmt"
)

func main() {
	name := "Alex"
	age := 21

	// Calling fmt.Sprintf() to format the string
	s := fmt.Sprintf("%s is %d years old.", name, age)

	// Prints the formatted string
	fmt.Println("s:", s)

	name = "Alvin Alexander"
	age = 43

	// Calling fmt.Sprintf() to format the string
	s = fmt.Sprintf("%s is %d years old.", name, age)

	// Prints the formatted string
	fmt.Println("s:", s)
}

Output:

s: Alex is 21 years old.
s: Alvin Alexander is 43 years old.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.