Don’t Go To Casting Hell; Use Default Native Types in Go
Tags: go, programming, tutorial, casting
Published: 2020-05-21 16:22:46

---
title: Don’t Go To Casting Hell; Use Default Native Types in Go
published: true
date: 2020-05-21 16:21:18 UTC
tags: Golang,Programming,Tutorial,casting
canonical_url: https://qvault.io/2020/05/21/dont-go-to-casting-hell-use-default-native-types-in-go/
---

 ![hell red sky](https://qvault.io/wp-content/uploads/2020/05/hrll-300x200.jpeg)

The post [Don’t Go To Casting Hell; Use Default Native Types in Go](https://qvault.io/2020/05/21/dont-go-to-casting-hell-use-default-native-types-in-go/) appeared first on [Qvault](https://qvault.io).

Go is strongly typed, and with that, we get many options for simple variable types like integers and floats. The problem arises when we have a _uint16_, and the function we are trying to pass it into takes an _int_. We find code riddled with _int(myUint16)_ that can become slow and annoying to read.

Go’s basic types are:

```go
bool

string

int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128
```

There are 5 different types that can represent an integer, 5 types for an unsigned integer, 2 for a float, and 2 for a complex number. While it’s hard to defend the notion that the compiler itself has default types, the standard library certainly plays favorites.

For example, the [cmplx](https://golang.org/pkg/math/cmplx/) package which does math with complex numbers accepts and returns exclusively _complex128_.

With floats, the vast majority of the [math](https://golang.org/pkg/math/) package has function signatures using _float64_. In the same package ints are usually just the _int_ type, and unsigned integers are typically _uint32_.

These are what I’ve come to refer to as the “default native types”:

```go
bool

string

int

uint32

byte

rune

float64

complex128
```

## Why Do We Care About Defaults?

There is a good reason that the majority of code uses these values. In all of the above cases, the choice of specific sub-types are based on range and precision. _int8_ can stores values between -128 and 127, while _int64_ ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. At the same time, _int8_ uses a single byte while _int64_ uses 8x that.

The defaults above were chosen in the standard library (and by the vast majority of Gophers) because they are the common-sense, works-most-of-the-time, big-enough-range values. Exposing a [rounding function](https://golang.org/pkg/math/#Round)for _float32_ simply won’t be as useful as _float64_. It can’t be used by as many values.

```go
func Round(x float64) float64
```

If you have a float32 that you want to round, you first need to cast it:

```go
math.Round(float64(myFloat32))
```

This is not only slow but clunky to read. Type conversions take time. Memory must be allocated. My advice is to use the default type (_float64_ in the case of floats) in your applications unless you have a compelling reason not to.

## When Not To Use Default Types

Performance and Memory.

That’s about it. The only reason to deviate from the defaults is to squeeze out every last bit of performance when you are writing an application that is resource-constrained. (Or, in the special case of uint64, you need an absurd range of unsigned integers).

For example, I probably wouldn’t swap out a single _uint32_ for _uint8_, even if I was certain I would only need 8 bytes. However, If I have a slice of uints that can potentially hold thousands of values, I may see a significant memory savings by doing a few type conversions and using _uint8_.

A good example of this are the packages I maintain, [go-tinydate](https://github.com/lane-c-wagner/go-tinydate), and [go-tinytime](https://github.com/lane-c-wagner/go-tinytime). Usually, I encourage users NOT to use them, and to just use the default [time.Time](https://golang.org/pkg/time/#Time). However, in my backend career, there have been applications that went from requiring 16GB of RAM down to <4GB by making the swap.

## Use Defaults

Make your life and the lives of your coworkers easy. Use the defaults unless you have a very compelling reason not to.

## Thanks For Reading

Hit me up on twitter [@wagslane](https://twitter.com/wagslane) if you have any questions or comments.

Follow me on Dev.to: [wagslane](https://dev.to/wagslane)

The post [Don’t Go To Casting Hell; Use Default Native Types in Go](https://qvault.io/2020/05/21/dont-go-to-casting-hell-use-default-native-types-in-go/) appeared first on [Qvault](https://qvault.io).