liveBook · Manning

Byte type

As explained in Go in Action, Second Edition

A byte is an eight-bit unsigned integer value used in Go to represent raw binary data.

A byte is Go’s meaningful name for uint8: an unsigned, fixed-width eight-bit integer ranging from 0 to 255. The alias signals that a value represents data rather than an ordinary numeric quantity. section 3.1

The distinction matters most when handling raw contents. Standard-library APIs commonly accept or return byte sequences as []byte, allowing code to process text or binary files without assuming a text encoding. section 3.1 section 2.6.4

Use byte-oriented values when:

  • Representing individual eight-bit data units or a slice of raw file contents. section 3.1 section 2.6.4
  • Passing data to APIs that operate on []byte rather than strings. section 3.1 section 2.6.4
  • Converting raw bytes to string only after deciding that the loaded file should be treated as text. section 2.6.4

Count words from raw file data

contents, err := os.ReadFile("notes.txt") if err != nil { return err } words := strings.Fields(string(contents)) os.ReadFile returns []byte because a file may contain binary data. For text, convert those bytes to string; for byte-oriented processing, use bytes.Fields(contents) instead. section 2.6.4

Table 3.1 Predeclared integer types

Type name

Description

Value vange

int

Signed integers

32 or 64 bits, depending on your architecture

int8

Signed 8-bit integers

-128 to 127

int16

Signed 16-bit integers

-32,768 to 32,767

int32

Signed 32-bit integers

-2,147,483,648 to 2,147,483,647

int64

Signed 64-bit integers

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

uint

Unsigned integers

32 or 64 bits, depending on your architecture

uint8

Unsigned 8-bit integers

0 to 255

uint16

Unsigned 16-bit integers

0 to 65,535

uint32

Unsigned 32-bit integers

0 to 4,294,967,295

uint64

Unsigned 64-bit integers

0 to 18,446,744,073,709,551,615

byte

An alias for uint8

rune

An alias for int32

Compare byte with uint8 and the other predeclared integer types to distinguish its representation from architecture-dependent integers. section 3.1

How Go in Action, Second Edition covers it

How deep it goes
Defines byte as uint8 and demonstrates []byte file handling, but defers broader string and Unicode treatment.
Examples
Uses os.ReadFile output, string conversion, strings.Fields, and bytes.Fields to process file contents.
What it assumes
Assumes familiarity with integer types and slices while establishing the distinction between raw data and numeric values.
What you can do after
Apply it via os.ReadFile and bytes APIs, converting []byte to string when the data is known to be text.
How it connects
Connects byte to uint8, slices, strings, file I/O, strings.Fields, and the bytes package.

Also explained in

Compare how 2 books explain byte type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.