in this post, we’ll show you how to format strings in Golang. Golang has a large number of string formatting options. The fmt is a golang package that can be used to format a string. This package has inbuilt find functions that implement formatted I/O.
To format strings in Go, we use the following functions:
- fmt.Printf
- fmt.Sprintf
- fmt.Fscanf
What’s fmt Package
The fmt package enables you to convert strings, numbers, and even objects to a particular string format. All of the formatting methods are parts of this package.
Format String Argument
- 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 – the shortest representation of %e or %f
- G – the shortest representation of %E or %F
- c – a character represented by the corresponding Unicode code point
- q – a quoted character
- U – Unicode escape sequence
- t – the word true or false
- s – a string
- v – default format
- #v – Go-syntax representation of the value
- T – a Go-syntax representation of the type of the value
- p – pointer address
- % – a double %% prints a single %
Go String Format Example
The fmt package provides a wide variety of string formatting functionalities. The formating method will format the string according to the specified format specifiers. Let’s look at a few popular string formatting examples in GoLang.
Format String Using Printf()
The fmt.Printf() method prints a formatted string to the console. The %s expects a string value and the %d an integer value.
Example:
package main
import (
"fmt"
)
func main() {
emp_name := "Rachel"
age := 17
salary := 123
fmt.Printf("My name is %s, and I am %d years old. My annual wage is %d dollars\n", emp_name, age, salary)
}
Output :
My name is Rachel, and I am 17 years old. My annual wage is 123 dollars
Format String Using Sprintf()
The fmt.Sprintf function formats a string into a variable.
Example:
package main
import (
"fmt"
)
func main() {
emp_name := "Rachel"
age := 17
salary := 123
formatted_str := fmt.Sprintf("My name is %s, and I am %d years old. My annual wage is %d dollars\n", emp_name, age, salary)
fmt.Println(formatted_str)
}
Output :
My name is Rachel, and I am 17 years old. My annual wage is 123 dollars
Formatted String using Fscanf() Method
The Go language’s fmt.Fscanf() method scans the supplied text, reads from r, and then stores the space-separated values into subsequent arguments according to the format.
Syntax:
func Fscanf(r io.Reader, format string, a …interface{}) (n int, err error)
It returns the number of items successfully parsed. The above function accepts three parameters:
- r io.Reader: The scanned specified texts are stored in this parameter.
- format string: This option specifies the various formats in which the elements should be received.
- a …interface: This is the variable that is specified for each element.
Example:
package main
import (
"fmt"
"os"
"strings"
)
func main() {
var (
emp_name string
age int
salary int
)
r := strings.NewReader("Rachel 23 123")
emp, err := fmt.Fscanf(r, "%s %d %d", &emp_name, &age, &salary)
if err != nil {
fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
}
fmt.Println(emp_name, age, salary)
fmt.Println(emp)
}
Output :
Rachel 23 123 3
