A closer look at Golang type system

A detailed look of the Go type system, with examples

Ankur Anand
Nov 29, 2018 · 8 min read

Why we need a type?

Before answering that, we need to look at some of the primitive abstracted layers of programming languages that we don’t usually deal with.

Image for post
Image for post
Binary 0s and 1s
Image for post
Image for post
What happens when you attain nirvana in computer science. (image source Martix movie)
Image for post
Image for post

So what is a type?

The notion of type does vary from programming language to programming language. It can be expressed in a number of different ways, but roughly they all have some sort of consensus.

  1. A set of operations on those values. For example, with a type of integer we can add ( + ) and subtract ( ). On the type of string we can concatenate, perform empty checks, and so forth.
  2. Typing is checked by the compiler and/or runtime to ensure the integrity of the data and interpret the data as meant by the developer.
  1. Type constructors — Way for a programmer to define new types.
    Eg. Pointer to T, where T is a Type or Struct {a: T}
  2. Type Inference — The compiler can infer the type of a variable or a function without us having to explicitly specify it. Go has Uni-directional type inference.
  3. Type Compatibility — Which assignments are allowed by the type system? a int; b int8; a = b; ?
    How to determine if two types are equal? In Go assignability is what mostly determines whether types can be used interchangeably. We will look at this later in details.

The type system in Go

There are some fundamental specs that govern the type system In Go. We will be looking at some of the important ones.

Image for post
Image for post
type system in go
Image for post
Image for post
Type system in Go.

Named types

Types with names such as int, int64, float32, string, and boolare predeclared. All of the predeclared boolean, numeric and string types are named types.

var i int // named type
type myInt int // named type
var b bool // named type

Unnamed types

Composite types — array, struct, pointer, function, interface, slice, map, and channel types — are all unnamed types.

[]string // unnamed type
map[string]string // unnamed type
[10]int // unnamed type

Underlying types

Each type, T , has an underlying type.

Image for post
Image for post
  • 5 and 7: We have a type literal, so the underlying type will be T itself — map[string]int and *N pointer. Note these type literals are also unnamed type
  • 4, 6, and 10: T's underlying type is the underlying type which T refers to in its type declaration. For example,B refers to A, hence B is a string.


This occurs when a variable v can be assigned to a variable to type T.

Image for post
Image for post
Assignability specs Golang.
8:4: cannot use ai (type aInt) as type int in assignment
9:13: cannot use i (type int) as type aInt in argument to printAiType

Type Conversion

Image for post
Image for post
Type conversion specs.

Type identity

Two types are either identical or different.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store