String formatting in GoLang

String formatting is essential when we need to create a string dynamically. String formatting makes our lives easier by allowing us to do things that can take much longer if done some other way. For example, we need to print a hex value of a number. Using string formatting it is a lot easier. In this post, we will dive deep into what can be done using string formatting in Go.

Different Ways to format string in GoLang

There are two main ways in which we can do string format.

  1. Using the Printf() function to print the formatted string.
  2. Using the Sprintf() function to just create and return the string instead of printing it to the console.

Both of these methods are available from the fmt package.

1. Using the Printf() function to format string in GoLang

The Printf() function can be used to print the formatted string to the console. So, let’s create formatted strings using the printf function.

1.1) Format the actual values

The actual values can be formatted in a simple and easy way. So, let’s start with the ints.

To print the ints we need to use %d.

fmt.Printf("%d", 42)    // 42

To print floats we can use %f as shown below.

fmt.Printf("%f", 12.5)    // 12.500000

Floats can also use scientific notation like this.

fmt.Printf("%e\n", 23000000.45)    // 2.300000e+07
fmt.Printf("%E\n", 23000000.45)    // 2.300000E+07

Floats can be formatted as we like. All we need to specify is the width of the float.

fmt.Printf("|%4.2f|\n", 1.23)    // |1.23|

Booleans are formatted using the %t formatter.

fmt.Printf("%t", true)    // true

The characters can be formatted using the %c formatter.

fmt.Printf("%c", 'a')    // a

To format a string we need the %s formatter.

fmt.Printf("%s", "a string")    // a string

// format the size of string
fmt.Printf("|%8s|", "apple")   // |   apple|

1.2) Format the encoded values

To format the binary, we need %b.

fmt.Printf("%b", 12)    // 1100

The hex value can simply be formatted using the %x formatter.

fmt.Printf("%x", 123)    // 7b

1.3) Get the type of value

To print the type of value we use the %T formatter (observe the capital T).

fmt.Printf("%T", 42)    // int

1.4) Format the pointer value

The pointers can be formatted using the %p formatter.

package main

import (
	"fmt"
)

func main() {
	var p *int        // declare as pointer
	var q int
	
	q = 42
	p = &q  // pointer to q
	
	// prints the memory address
	fmt.Printf("%p", p)    // 0x40e020
}

1.5) Format the struct

To print the value of the struct we can simply use %v.

The %+v will include the field names of the struct as well. And the %#v will print the Go-representation of the struct. Here is an example.

package main

import (
	"fmt"
)

type Tree struct{
	name string
}

func main() {
	cherry := Tree{"cherry"}
	
	// value representation of the struct
	fmt.Printf("%v\n", cherry)    // {cherry}
	
	// includes names inside struct
	fmt.Printf("%+v\n", cherry)   // {name:cherry}
	
	// the Go representation of the struct
	fmt.Printf("%#v\n", cherry)   // main.Tree{name:"cherry"}
}

2. GoLang String formatting using Sprintf() function

Sprintf function creates the string and returns. It is a way to create a formatted string without printing.

s1 := fmt.Sprintf("%d", 42)    // create the string
fmt.Println(s1)                // 42

These are the ways to create and print a formatted string in the Go programming language.