WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Basic · question 20 of 100

What is the purpose of the ’fmt’ package in Go? Could you give an example of its usage?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

The ‘fmt‘ package in Go is a frequently-used package that provides input-output facilities with functions for formatted I/O operations, as well as string manipulation. It’s part of the standard Go library, and its primary purpose is to format and print data in a human-readable form. Some of the main functionalities of the ‘fmt‘ package include:

1. Printing formatted data to the standard output (or other output streams).

2. Scanning formatted input from strings, files, or the standard input.

3. Formatting integer, floating-point, complex number, and string data.

Here is a simple example demonstrating some usages of the ‘fmt‘ package:

package main

import (
    "fmt"
)

func main() {
  var intVar int = 42
  var floatVar float64 = 3.14159

  fmt.Println("Simple output:")
  fmt.Println("Integer value:", intVar)
  fmt.Println("Float value:", floatVar)

  fmt.Printf("nFormatted output:n")
  fmt.Printf("Integer value: %dn", intVar)
  fmt.Printf("Float value: %.2fn", floatVar)

  str := fmt.Sprintf("Sprintf: Integer: %d, Float: %.2fn", intVar, floatVar)
  fmt.Println(str)
}

This program demonstrates three major usages of the ‘fmt‘ package:

1. **‘Println‘**: It prints the arguments separated by spaces and adds a newline at the end. It is commonly used to simply output data to the standard output.

2. **‘Printf‘**: It uses a format string to specify the format of the printed data. In the example above, ‘

3. **‘Sprintf‘**: It works similarly to ‘Printf‘, but instead of printing to the standard output, it returns the formatted string. This is particularly useful when you want to store the formatted output in a variable, as shown in the example above.

You can find more information and formatting options in the official Go ‘fmt‘ package documentation: [https://golang.org/pkg/fmt/](https://golang.org/pkg/fmt/)

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics