How to use the fmt package in Golang

Before discussing the fmt package, I would like to define what a package is.Technically, a Package is a collection of source filescodes in the same directory that are executedcompiled together.

fmt is a Go package that is used to format basic strings, values, inputs, and outputs. It can also be used to print and write from the terminal. Additionally, Go has formats called verbs. A verb is a placeholder for a named value to be formatted.

Let’s take a look at some of them:

  • %v represents the named value in its default format
  • %T represents the type of the value
  • %d expects value to be an integer type of base 10
  • %b expects value to be an integer type of base 2
  • %s the bytes of string or slice
  • %f expects value to have a float type

Required import for fmt package

In order to start using the fmt package, we will have to import it into our code:

 import "fmt"

Functions in the fmt package

Below are some of the functions the package provides, along with their descriptions:

fmt.Print()

fmt.Print() prints whatever it gets to the terminal without adding any space or newlines unless it’s unequivocally coded.

Spaces are added when the arguments are not strings.

package main
import "fmt"
func main() {
fmt.Print("Hello")
fmt.Print("World")
}

fmt.Printf()

fmt.Printf() provides custom formatting of input strings using one or more verbs and then prints the formatted string to the terminal without appending any space or newlines (unless explicitly coded).

package main
import "fmt"
func main() {
year := 2001
fmt.Printf("I was born in %d", year)
}

fmt.Println()

fmt.Println() is similar to fmt.Print(), the difference being that it adds spaces between arguments and appends a newline at the end.

package main
import "fmt"
func main() {
fmt.Println("Hello", "World")
fmt.Println("welcome to my world!")
}

fmt.Sprint()

The fmt.Sprint() function formats and returns the input string without printing anything to the terminal.

package main
import "fmt"
func main() {
a := fmt.Sprint("Hello World")
b := fmt.Sprint("Hello", "World")
fmt.Println(a)
fmt.Println(b)
}

In our code, notice how calling the fmt.Sprint() function doesn’t print anything. Instead, it returns two values that we store in variables “a” and “b”. In order to see the values, we have to use a print statement.

fmt.Sprintln()

fmt.Sprintln() is similar in function to fmt.Sprint(), except that it automatically adds spaces between arguments.

package main
import "fmt"
func main() {
a := fmt.Sprintln("Hello","World")
fmt.Println(a)
}

fmt.Sprintf()

fmt.Sprintf() is used to format an input string. It also works like fmt.Printf(), the significant distinction being that fmt.Sprintf() returns the value as opposed to printing it out.

package main
import "fmt"
func main() {
name := "Chisom"
s := fmt.Sprintf("My name is %s", name)
fmt.Print(s)
}

fmt.Scan()

fmt.Scan() collects user input from the standard input and stores this in successive arguments. Spaces or newlines are considered multiple values and are stored in multiple arguments. This function anticipates that an address of each argument should be passed.

package main
import "fmt"
func main() {
var name string
fmt.Println("What's your name?")
fmt.Scan(&name)
fmt.Println("Nice to meet you", name)
}

Enter the input below

fmt.Scanf()

fmt.Scanf() reads text from the standard input and puts away progressive space-separated values into progressive arguments as specified by the format. It also expects an address of each argument to be passed.

package main
import "fmt"
func main() {
var name string
fmt.Printf("What's your name: ")
fmt.Scanf("%s",&name)
fmt.Printf("Your name is %s\n", name)
}

Enter the input below

fmt.Scanln()

fmt.Scanln() functions similarly to fmt.Scan, but it stops scanning at a newline. This means that after the last item, there must be a newline or EOFEnd of File.

package main
import "fmt"
func main() {
var z int
fmt.Scanln(&z)
fmt.Printf("Scanln: Z = %d\n", z)
}

Enter the input below

The formatted I/O functions in Golang include, but are not limited to only the ones discussed above.

You can refer to the GO documentation for more.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved