use the fmt package to print a value of type byte in go

To print a value of type byte in Go using the fmt package, you can use the %v verb with the byte value as its corresponding argument.

main.go
package main

import "fmt"

func main() {
    var b byte = 65 // ASCII code for 'A'
    fmt.Printf("%v\n", b) // Output: 65
}
126 chars
9 lines

In the above code, we declared a variable b of type byte with a value of 65, which is the ASCII code for the uppercase letter 'A'. We then used the fmt.Printf function with %v verb and the b variable as its corresponding argument to print the value of b to the console.

Alternatively, you can also use the %d verb to print the byte value as an integer.

main.go
fmt.Printf("%d\n", b) // Output: 65
36 chars
2 lines

related categories

gistlibby LogSnag