Golang Conversions – Ints To Strings And Strong Typing
Tags: education, engineering, go, programming
Published: 2020-03-31 16:30:39

---
title: Golang Conversions – Ints To Strings And Strong Typing
published: true
date: 2020-03-31 14:46:11 UTC
tags: education,engineering,golang,programming
canonical_url: https://qvault.io/2020/03/31/golang-conversions-ints-to-strings-and-strong-typing/?utm_source=rss&utm_medium=rss&utm_campaign=golang-conversions-ints-to-strings-and-strong-typing
---

![](https://i1.wp.com/qvault.io/wp-content/uploads/2020/03/photo-1500589177368-c810ea3db799.jpeg?resize=742%2C493&ssl=1)

Go is a strongly typed language, which means at any point a developer should know exactly what **type** of value they are dealing with. For example, if we have a function that prints a string, we can’t just give it an integer and expect it to work. We have to cast it to a string explicitly:

```
func main() {
	num := 5
	numString := strconv.Itoa(num)
	printString(numString)
}

func printString(s string) {
	fmt.Println(s)
}
```

If we don’t cast the value, the go compiler won’t even let us compile the program.

## Dynamic Typing is Slow

Developers coming from dynamically typed languages often get annoyed with Go’s strong typing. They think that the compiler should just _know what they mean_ and do the type cast implicitly.

* * *

_Strongly typed languages won't guess for you. They make you make the decisions._  
[Click To Tweet](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwp.me%2Fpb7Qei-gg&text=Strongly%20typed%20languages%20won%27t%20guess%20for%20you.%20They%20make%20you%20make%20the%20decisions.&via=wagslane&related=wagslane)

* * *

There is a reason for this. Type conversions take time and resources. If the Go runtime were to dynamically type every value then programs would run a lot slower in general.

* * *

_If you want slow programs then go back to Python or Javascript._  
[Click To Tweet](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwp.me%2Fpb7Qei-gg&text=If%20you%20want%20slow%20programs%20then%20go%20back%20to%20Python%20or%20Javascript.&via=wagslane&related=wagslane)

* * *

## Strong Typing Is Explicit

In addition to strong typing being faster, strong typing allows the developer to know exactly what type of value they are dealing with. I can’t tell you how many times in Python I’ve had to run a program and print out what type of object something is. (Looking at you NumPy)

## Strong Typing Saves Memory

![](https://i0.wp.com/qvault.io/wp-content/uploads/2020/03/photo-1533279443086-d1c19a186416.jpeg?fit=742%2C417&ssl=1)

In one of our production apps, we were storing millions of **ints** in memory. Being on 64-bit machines, this means that we were storing 64 bits for each integer when in reality the integer stored was never greater than 10. By swapping out **ints** for **uint8s** we saved 80% of the memory that our application was using. The guy paying our cloud bill was quite happy about that.

While changing int and float types can save memory, beware of these kinds of optimizations. A program can become quite hard to read if every other line is:

```
toRound := float64(someNumber)
toSave := float32(toRound)
```

The truth is that most of the standard library functions and popular packages (and hopefully the stuff you write too) uses “default” sizes. For example, [math.Round](https://golang.org/pkg/math/#Round) uses **float64s** , and [time.AddDate](https://golang.org/pkg/time/#Time.AddDate) uses **ints**. Unless the memory savings are significant, it’s usually best to stick with “normal”.

## Interfaces – Not Duck Types

Interfaces allow for a kind of [polymorphism in Go](https://qvault.io/2020/03/15/best-practices-for-writing-clean-interfaces-in-go/). Their purpose is **not** to give developers a way to sneak dynamic typing into the language. I’ve seen developers do things like:

```
func jsonToDynamic(dat []byte) {
	m := map[string]interface{}{}
	json.Unmarshal(dat, &m)
	// do something with m["hello"]
}
```

I humbly ask… **WHY?** When unmarshalling JSON, 99% of the time we should know the shape of the object. If we know the shape of the object, then we should unmarshal into a struct where each field’s type is declared. We will even get a nice unmarshal error if the shape is malformed.

## 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 [Golang Conversions – Ints To Strings And Strong Typing](https://qvault.io/2020/03/31/golang-conversions-ints-to-strings-and-strong-typing/) appeared first on [Qvault](https://qvault.io).