Go fmt Package

Go fmt Package

fmt is pronounced “fumpt” and is one of Go’s core packages. It's mainly used for printing information to the terminal. fmt package has a broader purpose like helping us format data, for this reason, it's sometimes referred to as the format package.

The package provides:

  • Println(), Print(), Printf(): used to format and print data in various ways and use cases.

  • Sprint(), Sprintln(), Sprintf(): only formats the data and prints nothing

  • and the scan(): reads user input from the terminal.

The .Print() and .Println()

The Go fmt package gives two closely-related functions for formatting a string to be displayed on the terminal.

.Print() accepts strings as arguments and concatenates them without any spacing.

.Println(), on the other hand, adds a space between strings and appends a new line to the concatenated or a line break at the end

fmt.Print("My", "name", "is", "Lukman")

// MynameisLukman
fmt.Println("My", "name", "is", "Lukman")

fmt.Println("new", "line")

// My name is Lukman
// new line

The .Printf() function

The Go.Printf() function in fmt provides custom formatting of a string using one or more verbs. A verb is a placeholder for a named value (constant or variable) to be formatted according to these conventions:

  • %v represents the named value in its default format
  • %d expects the named value to be an integer type
  • %f expects the named value to be a float type
  • %T represents the type for the named value

but unlike .Println(), .Printf() does not append a newline to the formatted string.

name := "Lukman"
fmt.Printf("My name is %v", name)
// My name is Lukman

age := 90
fmt.Printf("I am %d years old", age)
// I am 90 years old

The fmt.Sprint() and fmt.Sprintln().

Unlike the .Print() and .Println() functions, the fmt package provides other functions that don’t print strings, but format them instead like

user := "Kenny"
Feedback := "Nice book!"
userFeedback := fmt.Sprint(user, "feedback on your book is", feedback)


fmt.Print(userFeedback)
// Prints: Kenny feedback on your book is Nice book!

Take a closer look at userFeedback and how calling fmt.Sprint() doesn’t print out anything. Rather, it returned a value that we store in userFeedback. When a value is returned, it means that a function did some computation and is giving back the computed value. Afterward, we can use the returned value for later usage. we’ve formatted one string by concatenating four separate strings. To see the value of userFeedback, we have to use a print statement.

fmt.Sprintln() works like fmt.Sprint() but it automatically includes spaces between the arguments for us (just like fmt.Println() and fmt.Print()):

quote = fmt.Sprintln("see here,", "no spaces!")
fmt.Print(quote) // Prints see here, no spaces!
Even though we didn’t add a trailing space in "see here," or a leading space in "no spaces!", quote is concatenated with a space in between: "see here,", "no spaces!".

The Sprintf function

when we have to interpolate a string, without printing it, then we can use fmt.Sprintf().

Just like fmt.Printf(), fmt.Sprintf() can also use verbs:

user := "userA"
winner := fmt.Sprintf("The winner is… %v!", user)

fmt.Print(answer) // Prints: The winner is is… userA!

fmt.Sprintf() works very similarly to fmt.Printf(), the major difference is that fmt.Sprintf() returns its value instead of printing it out!

Go Fmt .Scan() Function

The Go fmt .Scan() function scans user input from the terminal and extracts text delimited by spaces into successive arguments. A newline is considered a space. This function expects an address of each argument to be passed.

package main
import "fmt"

func main() {
var name string
var age int
fmt.Println("What's your name?")
fmt.Scan(&name)

fmt.Println("and what's your age?")
fmt.Scan(&age)

fmt.Printf("%v is %d years old!", name, age)
}


//$ What's your name?
//$ Lukman
//$ now what's your age?
//$ 90
//$ Lukman is 90 years old!

we have looked into seven functions exposed from the go fmt package, now you can fully utilize the fmt package in your Go applications.

now it's time to harness the full power of go

molang.gif

what next: check out the [official documentation] (golang.org/pkg/fmt) to learn more about these functions.