How To Format a String in Golang

How To Format a String in Golang?

In this post, we will guide you through the process of formatting strings in Golang. Golang provides a rich set of string formatting options, and the key player in this area is the fmt package. This package includes built-in functions for formatted input/output.

Overview of Formatting Functions

To format strings in Go, you can leverage the following functions from the fmt package:

  • fmt.Printf
  • fmt.Sprintf
  • fmt.Fscanf

Understanding the fmt Package

The fmt package in Golang empowers you to convert strings, numbers, and objects into specific string formats. All the formatting methods discussed here are part of this package.

Format String Arguments

Here is a quick reference to some commonly used format specifiers:

  • %d: Decimal integer
  • %o: Octal integer
  • %O: Octal integer with 0o prefix
  • %b: Binary integer
  • %x: Hexadecimal integer lowercase
  • %X: Hexadecimal integer uppercase
  • %f: Decimal floating point, lowercase
  • %F: Decimal floating point, uppercase
  • %e: Scientific notation (mantissa/exponent), lowercase
  • %E: Scientific notation (mantissa/exponent), uppercase
  • %g: Shortest representation of %e or %f
  • %G: Shortest representation of %E or %F
  • %c: Character represented by the corresponding Unicode code point
  • %q: Quoted character
  • %U: Unicode escape sequence
  • %t: Word true or false
  • %s: String
  • %v: Default format
  • #v: Go-syntax representation of the value
  • %T: Go-syntax representation of the type of the value
  • %p: Pointer address
  • %%: Double %% prints a single %

Go String Format Examples

The fmt package in Golang offers a wide range of string formatting functionalities. Let’s explore a few popular examples:

Using Printf()

The fmt.Printf() method prints a formatted string to the console. For example:

package main

import (
	"fmt"
)

func main() {
	empName := "Rachel"
	age := 17
	salary := 123
	fmt.Printf("My name is %s, and I am %d years old. My annual wage is %d dollars\n", empName, age, salary)
}
// Output: My name is Rachel, and I am 17 years old. My annual wage is 123 dollars

Using Sprintf()

The fmt.Sprintf function formats a string into a variable. Example:

package main

import (
	"fmt"
)

func main() {
	empName := "Rachel"
	age := 17
	salary := 123
	formattedStr := fmt.Sprintf("My name is %s, and I am %d years old. My annual wage is %d dollars\n", empName, age, salary)
	fmt.Println(formattedStr)
}
// Output: My name is Rachel, and I am 17 years old. My annual wage is 123 dollars

Using Fscanf()

The fmt.Fscanf() method in Golang scans the supplied text, reads from r, and stores the space-separated values into subsequent arguments according to the format. Example:

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	var (
		empName string
		age     int
		salary  int
	)

	r := strings.NewReader("Rachel 23 123")

	emp, err := fmt.Fscanf(r, "%s %d %d", &empName, &age, &salary)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
	}
	fmt.Println(empName, age, salary)

	fmt.Println(emp)
}
// Output: Rachel 23 123 3

And that’s it! We hope this guide has provided you with a clear understanding of the string formatting process in Golang. Feel free to experiment with different format specifiers and methods to suit your specific needs.

Check Also: