In Go, you can use the fmt
package to format and print data with a specific radix (binary, hexadecimal, etc.).
To format and print an integer with a specific radix, you can use the %b
format specifier for binary, %d
for decimal, %x
for hexadecimal, and %o
for octal.
Here's an example to show how to format and print a number with different radices:
package main
import "fmt"
func main() {
number := 42
// Print the number in binary
fmt.Printf("Binary: %b\n", number)
// Print the number in hexadecimal
fmt.Printf("Hexadecimal: %x\n", number)
// Print the number in octal
fmt.Printf("Octal: %o\n", number)
}
Output:
Binary: 101010
Hexadecimal: 2a
Octal: 52
You can format and print other types of data, like floating-point numbers and strings, using different format specifiers provided by the fmt
package.