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 in to 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, since they are the building blocks for all of 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 single program you write. If you think of writing software like building a house, the types in this chapter are like the nails and the screws that hold everything together.

In this chapter you’ll learn more about these fundamental types, as well as some tips on using them most effectively.

3.1 Integer Types

Integer types store whole numbers of a certain range, depending on their size. In Go we have two varieties of integer — signed and unsigned — along with several different size variations for each, along with a couple of alias types which declare alternate names for certain circumstances. Integer types are very common in Go, and are used for basic math, array and slice indices, and as configuration arguments to 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

3.8.1 Interpreted Strings

3.8.2 Raw String Values

3.8.3 String Operations

3.8.4 len() And Unicode Characters

3.8.5 Iterating Unicode Strings Using A range Loop