liveBook · Manning

Data type

As explained in Go in Action, Second Edition

A type is a specification of the values and operations permitted for a piece of data in a programming language.

A data type specifies the values and operations permitted for data. Go combines primitive types with user-defined types to model domain concepts safely and expressively. section 5.0

Named types give reusable structure to related values. A person type can group a name and age, making declarations clearer than repeating an anonymous structure. section 3.6.1

Modeling people with a struct type

type person struct { name string; age int }; andy := person{name: "Andy", age: 42}; james := person{name: "James", age: 26}. Named struct values can then be accessed through fields such as andy.name and andy.age. section 3.6.1

Go’s type system supports domain modeling through structs, type aliases, interfaces, and composition. Compile-time checking catches invalid assignments early, while composition builds complex types from simpler ones rather than inheritance. section 5.0

Types also constrain collection and representation details:

  • Treat an array’s length as part of its type: [4]int and [5]int are distinct and cannot be assigned to one another. section 4.1.2
  • Use len() to obtain an array’s complete capacity, including positions that were not explicitly initialized. section 4.1.2
  • Use pointers, type assertions, and type switches when managing references or checking concrete types at runtime. section 5.0
Listing 3.22 Using a type in a declaration
type person struct {
	name string
	age  int
}

andy := person{
	name: "Andy",
	age:  42,
}
james := person{
	name: "James",
	age:  26,
}
fmt.Println(andy.name, andy.age)   // output: Andy 42
fmt.Println(james.name, james.age) // output: James 26

Inspect the complete named-struct declaration and its concrete values. section 3.6.1

How Go in Action, Second Edition covers it

How deep it goes
Explains named structs, composition, compile-time checking, runtime type checks, and array identity, but defers detailed type declarations.
Examples
Demonstrates a person struct, named person values, field access, incompatible array lengths, and len() calls.
What it assumes
Assumes familiarity with primitive values and introduces composite types as a way to represent concepts beyond primitives.
What you can do after
Build it from scratch by defining named structs and recognizing how Go’s type rules constrain assignments and collections.
How it connects
Connects primitive types with structs, type aliases, interfaces, composition, pointers, type assertions, type switches, and arrays.

Also explained in

Compare how 18 books explain data 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.