Overview

3 Primitive Types And Operators

This chapter introduces Go’s primitive types as the basic building blocks you use in nearly every program. It explains how Go’s statically typed nature shapes the way you declare values, choose types, and work with literals, zero values, and conversions. The focus is on practical use: understanding what each type represents, how it behaves in memory, and how to avoid common mistakes that can lead to subtle bugs.

For numeric values, the chapter covers signed and unsigned integers, floating-point numbers, and complex numbers, along with the operators that work on them. It emphasizes choosing the simplest type that fits the task, usually int for general integers and float64 for floating-point work, while being cautious with unsigned integers because of wraparound behavior and conversion pitfalls. It also discusses how special floating-point values like infinity and NaN arise, and how arithmetic and bitwise operators behave differently across numeric types.

The chapter also explains bool, structs, pointers, and strings. Booleans are used for clear yes-or-no logic, structs group related data into reusable user-defined types, and pointers let you refer to and modify values indirectly through memory addresses. Strings are treated as read-only byte sequences encoded in UTF-8, so the chapter shows why byte length and character count can differ, how to iterate correctly over Unicode text with range or rune slices, and how rune helps represent and inspect individual characters.

Integer Type Sizes In Memory
numeric types
Unsigned Integer Wraparound
wraparound
Right Shifting In Signed And Unsigned Integer
bit shift
Floating-point Type Sizes In Memory
float types
Complex Type Sizes In Memory
float types
An Uninitialized Pointer Variable
pointer variable visualized
The Address Operator Returns The Memory Address Of A Variable.
pointer assignment visualized
intPtr "Points To" intVar.
pointer assignment visualized
Pointer Dereference Vs. Pointer Value
pointer variable visualized
Visualizing Character Size In Bytes
character size visualized

Summary

  • Go provides a number of built-in primitive types for common computing tasks.
  • There are several different integer types, however int is the most flexible, and the one you should use for most tasks.
  • bool types are useful in constructing conditional logic, and can make complex checks easier when used as return values.
  • Struct types group related values together, and are used in defining new types.
  • Go supports pointer types, allowing you to pass references to data without copying it.
  • Strings are read-only arrays of bytes that can contain UTF-8-encoded characters from any language.

FAQ

What are primitive types in Go, and why are they important?Primitive types are Go’s built-in fundamental data types, such as numbers, booleans, strings, pointers, and structs. They are important because they are the building blocks for most other types and are used in nearly every Go program.
What is the difference between signed and unsigned integer types?Signed integers can represent both positive and negative values, while unsigned integers can represent only zero and positive values. Signed types use two’s complement representation, and unsigned types offer a larger positive range for the same number of bits.
When should I use int versus a sized integer like int64 or uint8?Use int for most general-purpose integer work because it is the default literal type, commonly used by libraries, and typically the most efficient on your architecture. Use sized types like int64 or uint8 when you need a specific memory size, compatibility with APIs, or precise bit manipulation.
Why can unsigned integers be risky in Go?Unsigned integers can wrap around when they go below zero, producing very large values instead of negative numbers. This can also create subtle bugs when converting a negative signed integer to an unsigned one.
What are the main floating-point types in Go?Go provides float32 and float64. float32 is single-precision and uses 32 bits, while float64 is double-precision and uses 64 bits, giving greater accuracy.
How do floating-point literals work in Go?You can write floating-point literals using a decimal point or scientific notation, such as 12., .1, or 12e0. If you want a non-default float type, you can use an explicit conversion like float32(12.1).
What special floating-point values does Go support?Go can produce +Inf, -Inf, and NaN for overflow, division by zero, or invalid math operations. You can check for these values using functions from the math package, such as math.IsInf and math.IsNaN.
How do complex numbers work in Go?Go has built-in complex types: complex64 and complex128. They can be created with complex() or complex literals like 1 + 2i, and their parts can be extracted with real() and imag().
What is the difference between an interpreted string and a raw string?An interpreted string uses double quotes and supports escape sequences like \n and Unicode escapes. A raw string uses backticks, preserves characters literally, may span multiple lines, and does not process escape sequences.
How do pointers, structs, and runes help with data handling in Go?Structs group related fields into one value, pointers let you reference and modify data by address, and runes represent Unicode code points as int32 values. Together they help Go manage structured data and text efficiently, especially when working with Unicode strings.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free