Go from the beginning - converting between types
Tags: go, programming, beginners, tutorial
Published: 2022-06-08 21:40:54

---
title: Go from the beginning - converting between types
published: true
description: In this fourth part, we are looking at converting between different data types
series: go-beginners
tags: go, programming, beginners, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wghwpn62gnmgcbrvp4n.jpg
---

> TLDR; this article covers how to convert between strings and numbers

There's different data types and a need to convert between them. For example, we often need to convert between text and numbers for presentational and other reasons. We also need to convert between numbers and decimals without losing information in the process.

The main package for dealing with conversions in Go is `strconv`.

{% embed https://www.youtube.com/watch?v=jv81dMj2mgo %}

## Use case - command line arguments

Let's show a common case where you start off with strings and you need to make it into numbers, command line arguments. To use command line arguments in a program, you need the `os` package.  

`os.Args` points to an array representing your command line arguments. To access a specific argument, you would use the index operator `[]` like so:

```go
arg := os.Args[1]
```

What if you started this program like so:

```bash
go run main.go 1
```

### Finding the type

What type is `arg` in our code above? There are some ways to find out:

- **IDE**, if you use for example Visual Studio code and the Go plugin, hovering over the code, it will tell you that `os.Args` is a string array, `string[]`.
- **Type coercion**, you could try to modify that code and coerce it to be an integer like so, now what?

    ```go
    var no int = os.Args[1]
    ```

    You get an error:

    ```output
    cannot use os.Args[1] (type string) as type int in assignment
    ```

- **Use reflection**. Another way to find the above oout would have been to use the `reflect` package like so:

    ```go
    package main
    
    import (
      "reflect"
      "fmt"
      "os"
    )
    
    func main () {
      arg := os.Args[1]
      fmt.Println(reflect.TypeOf(arg))
    }
    ```

    Now, the program will print "string" as the type.

### Addressing the problem with `strconv`

Ok, so we know what type something is, what if we need to use these command line arguments, which are string and feed them into let's say a calculator program? 

Consider the below code, that at present WOULDN'T compile:

```go
package main

import (
  "fmt"
  "os"
)

func add(first int second) int {
  return first + second
}

func main() {
  add(os.Args[1], os.Args[2]) // this would NOT compile
}
```

The reason is that the values on `os.Args[1]` and `os.Args[2]` are `string` not `int`. To fix this issue, we need to use the conversion package `strconv`.

## Convert from string to int with `strconv`

To convert strings to integer, we need to use `strconv` and call the `Atoi()` (stands for Ascii to integer) function like so:

```go
package main

import (
 "fmt"
 "os"
 "strconv"
)

func add(first int, second int) int {
 return first + second
}

func main() {
 no1, _ := strconv.Atoi(os.Args[1])
 no2, _ := strconv.Atoi(os.Args[2])
 var sum = add(no1, no2) 
 fmt.Println(sum)
}
```

Note `_`, this is a *don't care* symbol. What happens when you call `Atoi()` is that it returns two things, the number and an error, if it fails.

### Handling conversion error

To handle an error, we need to store it in a variable, `err` and inspect it. If it's not `nil`, then we have an error.

Here's how we could encode that behavior below:

```go
package main

import (
 "fmt"
 "os"
 "strconv"
)

func main() {
 no, err := strconv.Atoi(os.Args[1])
 fmt.Println(no)
 if err != nil {
  fmt.Println(err)
  fmt.Println("Couldn't convert: " + os.Args[1])

 } else {
  fmt.Println(no)
 }
}
```

Try compile the above program and run it like so:

```bash
main 1 # 1
main hi # trconv.Atoi: parsing "hi": invalid syntax, Couldn't convert: hi
```

## Parse string to int

There's another way to convert a string to a int. That's by using the `ParseInt()` method. It does more than converting though, it does two things in fact:

- **base**, you can select according to what base to interpret the number as.
- **size**, bit size, from 0 to 64.

The syntax for the method looks like so:

```go
ParseInt(<s string>, <base int>, <bit int>) (int64, error)
```

Here's some examples:

```go
package main

import (
 "fmt"
 "reflect"
 "strconv"
)

func main() {
 var no int = 100
 fmt.Println(reflect.TypeOf(no))

 var intStr string = "100"
 fourBaseEightBitInt, _ := strconv.ParseInt(intStr, 4, 8)    // becomes no 16 and int64
 tenBaseSixteenBitInt, _ := strconv.ParseInt(intStr, 10, 16) // no 100,  and int64
 fmt.Println(reflect.TypeOf(fourBaseEightBitInt))
 fmt.Println(reflect.TypeOf(tenBaseSixteenBitInt))
}
```

## Integer to string

You might be dealing with the opposite; you have an integer, and you want it to be a string. In this case, you can use the `Itoa()` function, integer to ascii. Here's an example:

```go
var noOfPlayers = 8
str, _ := strconv.Itoa(noOfPlayers)

```

## Additional parsing

The `strconv` library is what you want if you start with a string and you want to convert to and from another format. Learn more about [strconv library here](https://pkg.go.dev/strconv)

## Summary

In this article, you learned how to convert between strings and number and some functions that are available to you in the standard library `strconv`.