- Published on
Print Formatting in go (golang)
- Authors
- Name
- Martin Karsten
- @mkdevX
If you come from other programming languages like JavaScript, the concept of having to add percentage signs when printing values might seem foreign to you. But in other programming languages, you often have to add a percentage sign followed by a letter. That is also how Print Formatting works in go (golang).
The symbol we use in every format specifier is %. Followed by letter with specific letter
Format Ppecifiers in C
The concept of Format specifiers comes from the C programming language. Format specifiers in C are used to take inputs and print the output of its type. The symbol we use in every format specifier is %. Format specifiers tell the compiler about the type of data given or input and the type of data that must be printed in the console.
Format Ppecifiers in go (golang)
Go uses almost the same format specifiers as C but slightly simplifies them.
Here are a few examples
General
SPECIFIER | PURPOSE |
---|---|
%v | the value in a default format |
%#v | a Go-syntax representation of the value |
%T | a Go-syntax representation of the type of the value |
%% | a literal percent sign; consumes no value |
Integer
SPECIFIER | PURPOSE |
---|---|
%b | base 2 |
%c | the character represented by the corresponding Unicode code point |
%d | base 10 |
%o | base 8 |
%O | base 8 with 0o prefix |
%q | a single-quoted character literal safely escaped with Go syntax. |
%x | base 16, with lower-case letters for a-f |
%X | base 16, with upper-case letters for A-F |
%U | Unicode format: U+1234; same as "U+%04X" |
String
SPECIFIER | PURPOSE |
---|---|
%s | the uninterpreted bytes of the string or slice |
%q | a double-quoted string safely escaped with Go syntax |
%x | base 16, lower-case, two characters per byte |
%X | base 16, upper-case, two characters per byte |
Default formator for %v
TYPE | DEFAULT |
---|---|
bool | %t |
int, int8 etc. | %d |
uint, uint8 etc. | %d, %#x if printed with %#v |
float32, complex64, etc | %g |
string | %s |
chan | %p |
pointer | %p |
Example: %T will print the type of a variable
import (
"fmt"
)
func main() {
num := 1
fmt.Printf("The variable num is of type %T \n", num)
}
The variable num is of type int