Golang String Formatting
Overview
The string is a slice of bytes enclosed in “ “. The string can be created by enclosing its contents in “”. The string is immutable which means once a string is created it's not possible to change it.
Introduction
The golang format string is very important when it comes to creating a dynamic string. The golang format string makes it easier for developers to do things that would otherwise take much longer to complete. For instance, when we have to print a hex value of a number. By using string formatting, it is a lot easier to convert the value to hex. There are 2 ways in which the strings in the go language can be formatted.
- To print the formatted string, we can use the Printf() function
- For creating and returning the string instead of printing it to the console, we can use the Sprintf() function.
Both of these methods are available in the fmt package.
String Formatting Functions in the fmt Package
Printf
The Printf function is used for printing the formatted string to the console. Let's create a formatted string by using the printf function.
Sprintf
The Sprintf function is used for creating and returning the string in golang. It is used for creating a formatted string without having to print it.
Fprintf
The Fprintf function in the go language is used for formatting according to the format specifier mentioned in the argument. This function comes under the package fmt and hence it needs to be imported before use. To understand this better, let’s take an example.
Golang program to illustrate the usage of the fmt.Fprintf() function
Output:
Explanation:
In this example, the Fprintf function is used for printing the sentence at the output. The Print function is used in case of any error.
Fscanf
The Fscanf function is used for scanning the text which is specified in the argument. It reads the text from the reader and then stores the separated values in the idea that is given in the format. To understand this better, let's take an example.
Golang program to illustrate the usage of the fmt.Fscanf() function
Output:
Explanation:
In this example, the Fscanf reads the input given in the code and scans and prints the text at the output. Here the inputs passed are 3 and hence n=3 is printed at the end of the execution.
The String Formatting Verbs
The go language has an abundance of formatting verbs that can be used along with the string functions of formatting. The table given below shows all the formatting verbs of the string in golang.
Verb | Description |
---|---|
%v | print the value in default type |
%d | int, int8, etc. |
%t | Used for boolean values |
%g | float values |
%d, %#x if printed with %#v | uint, unit8, etc |
%P | Used for pointer |
%s | Used for string |
%b | Base 2 |
%d | Used for base 10 |
%c | Used for representing a character by using the Unicode |
%o | Used for base 8 |
%O | Used for base 8 with 0o prefix. |
%x | Used for base 16, generally, for lower case letters, a-f |
%X | Used for base 16, generally, for upper case letters, A-F |
%q | It is used for single quote character literal |
%U | Used for Unicode format, generally U+1234; same as "U+%04X" |
%s | Used for the uninterpreted bytes of the slice or string |
%q | It is used for a double-quoted string that is safely escaped with Go syntax |
Different Ways to Format String in GoLang
Using the Printf() Function to Print the Formatted String
Formatting the actual values:
The formatting of actual values can be done easily and simply. We will first start with the int.
For printing the int, we will use the %d
For printing the float values, we will use the %f as shown in the example given below:
For floats, we can also use scientific notation as given below:
Float can also be formatted differently, for example
In this, the width of the float is to be specified. The boolean values can be formatted by using %t as shown below
The characters in the string can be formatted by using the %c formatter as shown in the example given below:
For the golang format string, we can use the %s formatter as shown in the example given below:
format the size of string
Formatting the encoded values:
To format the binary values, we can use %b as shown in the example given below:
The hex value can be formatted by using the %x formatter as shown in the example given below:
Getting the type of value:
To print the type of the value, we can use %T (here the T is capital) as shown in the example given below:
Format the Pointer Value
We can also format the pointer value by using the %p formatter as shown in the example given below:
Format the Struct
To format the structure or the struct in golang format string, we can use %v.
This includes the field name of the struct.
Using the Sprintf() Function to Just Create and Return the String Instead of Printing It to the Console
The Sprintf function is used for creating and returning the string in golang. It is used for creating a formatted string without having to print it. Let's take an example to understand it better.
In this example, by using the Sprintf, the string is created and assigned to variable s1 after which it is printed at the output.
Conclusion
- The string is a slice of bytes enclosed in “ “. The string can be created by enclosing its contents in “”. String are immutable which means once a string is created it's not possible to change it.
- The golang format string is very important when it comes to creating a dynamic string. The golang format string makes it easier for developers to do things that would otherwise take much longer to complete. For instance, when we have to print a hex value of a number. By using string formatting, it is a lot easier to convert the value to hex.
- There are 2 ways in which the strings in the go language can be formatted.
- To print the golang format string, we can use the Printf() function
- For creating and returning the string instead of printing it to the console, we can use the Sprintf() function.