Go Type

Overview of Go Type System

https://go101.org/article/type-system-overview.html

Basic Types

Built-in string type: string.
Built-in boolean type: bool.
Built-in numeric types:
int8, uint8 (byte), int16, uint16, int32 (rune), uint32, int64, uint64, int, uint, uintptr.
float32, float64.
complex64, complex128.
Note, byte is a built-in alias of uint8, and rune is a built-in alias of int32. We can also declare custom type aliases (see below).

Composite Types

pointer types - C pointer alike.
struct types - C struct alike.
function types - functions are first-class types in Go.
container types:

  1. array types - fixed-length container types.
  2. slice type - dynamic-length and dynamic-capacity container types.
  3. map types - maps are associative arrays (or dictionaries). The standard Go compiler implements maps as hashtables.

channel types - channels are used to synchronize data among goroutines (the green threads in Go).
interface types - interfaces play a key role in reflection and polymorphism.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Assume T is an arbitrary type and Tkey is
// a type supporting comparison (== and !=).

*T // a pointer type
[5]T // an array type
[]T // a slice type
map[Tkey]T // a map type

// a struct type
struct {
name string
age int
}

// a function type
func(int) (bool, string)

// an interface type
interface {
Method0(string) int
Method1() (int, bool)
}

// some channel types
chan T
chan<- T
<-chan T

Type Definitions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Define a solo new type.
type NewTypeName SourceType

// Define multiple new types together.
type (
NewTypeName1 SourceType1
NewTypeName2 SourceType2
)

// The following new defined and source types
// are all basic types.
type (
MyInt int
Age int
Text string
)

// The following new defined and source types are
// all composite types.
type IntPtr *int
type Book struct{author, title string; pages int}
type Convert func(in0 int, in1 bool)(out0 int, out1 string)
type StringArray [5]string
type StringSlice []string

func f() {
// The names of the three defined types
// can be only used within the function.
type PersonAge map[string]int
type MessageQueue chan string
type Reader interface{Read([]byte) int}
}

Type Alias Declarations

1
2
3
4
5
6
7
type (
Name = string
Age = int
)

type table = map[string]int
type Table = map[Name]Age

Defined Types vs. Non-Defined Types

A defined type is a type defined in a type definition.

All basic types are defined. A non-defined type must be a composite type.

In the following example. type alias C and type literal []string both represent the same non-defined types, but type A and type alias B both represent the same defined type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type A []string
type B = A
type C = []string

// Type A has String method
func (a A) String() string {
return "a"
}

// Nope: can not declare method for alias-type
func (b B) String() string {
return "b"
}

// Nope: can not declare method for alias-type
func (c C) String() string {
return "c"
}