chapter three

3 Primitive types and operators

 

This chapter covers

  • Numeric types and numeric operations
  • The bool type
  • Structs
  • Pointers
  • Strings, runes, and string manipulation

Go comes with a number of types built into the language for some of the most common computing tasks, such as working with numbers, strings, and Boolean types. In a statically typed language like Go, these built-in types are often called primitive types because they are the building blocks for all the other types in the language.

Primitive types are the most fundamental tools in your toolbelt when building software with Go, and you will use them in every program you write. If you think of writing software as building a house, the types in this chapter are like the nails and screws that hold everything together.

In this chapter, you’ll learn more about these fundamental types, along with some tips for using them most effectively.

3.1 Integer types

Integer types store whole numbers within a certain range, depending on their size. In Go, we have two varieties of integer—signed and unsigned, which are outlined in table 3.1—and there are several different size variations for each, as well as a couple of alias types that declare alternate names for certain circumstances. Integer types are very common in Go and are used for basic math, array and slice indices, and configuration arguments for many of the standard library functions.

3.1.1 Choosing an integer type

3.2 Floating-point number types

3.2.1 Special floating-point values

3.2.2 Choosing a floating-point type

3.3 Complex number types

3.4 Mathematical operators

3.5 The bool type

3.5.1 Boolean functions

3.6 Struct types

3.6.1 Structs as types

3.7 Pointer types

3.7.1 Dereferencing pointers

3.7.2 Creating pointers with new and literals

3.8 The string type

Summary