Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Go, a byte is a built-in type alias for uint8. It is an unsigned 8-bit integer representing a value from 0 to 255. By convention, the Go compiler and standard library use byte to distinguish raw binary data and ASCII characters from standard numeric operations, even though byte and uint8 are strictly identical at compile time.

Type Characteristics

  • Underlying Type: uint8
  • Memory Footprint: 1 byte (8 bits)
  • Value Range: 0 to 255 (0x00 to 0xFF)
  • Default Zero Value: 0

Syntax and Initialization

A byte can be initialized using standard integer literals, character literals (enclosed in single quotes), hexadecimal literals, or binary literals.
// Explicit declaration using a decimal integer
var a byte = 65

// Character literal (untyped constant implicitly converted to byte)
var b byte = 'A'

// Hexadecimal literal
var c byte = 0x41

// Binary literal
var d byte = 0b01000001
Note: Character literals in Go are untyped constants representing Unicode code points (defaulting to rune / int32). When assigned to a byte, the compiler verifies that the constant’s integer value fits within the 8-bit bounds (0-255). Assigning a character whose Unicode code point exceeds 255 (e.g., 'Ā' which is U+0100 / 256, or '😊') to a byte will result in a compile-time overflow error.

Type Equivalence

Because byte is an alias and not a distinct, newly defined type, it is completely interchangeable with uint8. No explicit type conversion is required when assigning or comparing byte and uint8 variables.
var rawData byte = 200
var numericData uint8 = 200

// Valid: Direct comparison and assignment without casting
if rawData == numericData {
    numericData = rawData
}

Interaction with Strings

In Go, a string is fundamentally a read-only slice of bytes ([]byte). The language provides built-in syntax for converting between strings and byte slices, which allocates new memory and copies the underlying array to maintain string immutability.
// Converting a string to a byte slice
str := "Go"
bytes := []byte(str) // []byte{71, 111}

// Converting a byte slice back to a string
originalStr := string(bytes)

Formatting Verbs

When using the fmt package, a byte can be formatted in several ways depending on the desired representation of the 8-bit value:
var b byte = 65

fmt.Printf("%d", b) // Decimal: 65
fmt.Printf("%c", b) // Character: A
fmt.Printf("%x", b) // Hexadecimal: 41
fmt.Printf("%b", b) // Binary: 1000001
Master Go with Deep Grasping Methodology!Learn More