Image from: https://s3-ap-southeast-2.amazonaws.com/chrisdlangton/ghost/2017/12/golang-gopher.jpg
Image from: https://s3-ap-southeast-2.amazonaws.com/chrisdlangton/ghost/2017/12/golang-gopher.jpg

Type conversions in GoLang

ArkaprabhaM
3 min readAug 16, 2020

--

Integers to string

Welcome to another GoLang tutorial !

(If you don’t remember the basic syntax, go through the Introduction on my page and come back quick.)

Today we are going to look at a few type conversions between the two most common basic data types — integers and strings.

  • Integers are broadly classified into signed and unsigned, with further divisions based on the size, i.e, number of bits.
  • Signed integers can store negative values whereas unsigned can only store positive values but instead the positive range is doubled (ints range from -32768 to 32768, whereas units range from 0 to 65536)
  • The four sizes are: 8, 16, 32 and 64 bits — referring to more storage and which is out of scope for this tutorial.

That is all the pre-requisite knowledge you’ll need.

Open your text editor file and let’s get started.

First, we import the package main as always, and import our modules (on separate lines).

> package main

> import (“fmt”

“strconv”

)

Next, we’ll define our main function, and remember to put the inputs in the arguments and the return type:

> func main( x int64 ) string {

}

The module for all types of integer — string conversions is strconv.

All code snippets are to be put inside the main function. Please note that we should refrain from fmt.Println() except when we’re debugging. It serves as a good habit.

The first function we’ll learn is called Itoa:

> s := strconv.Itoa(250)

>return s

Itoa stands for Integer to ASCII, however we can easily remember it by “integer to alphabet”.

The Itoa function is narrow in its approach, in that it takes in a value of int only. That means if we have a variable that is of another type, say int64 or int32, we get an error. We can however circumvent this with a simple type conversion. However, with uint values, it may lead to information loss due to the reduced range.

> func main(x int32) {

t := strconv.Itoa(int(x))

return t

}

> var n int32 = 2020

> main(n)

The next function is called FormatInt. This has much wider application, as we’ll see below.

> t := strconv.FormatInt(x,10)

>return t

FormatInt takes input as int64 specifically, and is able to convert it into any base (2 being binary, 10 being decimal, 16 being hexadecimal and so on), and then converts it into a string.

Similarly, we also have a FormatUint to perform a similar conversion on unsigned integers.

Well, that’s all the ways you can convert an integer into a string. As a last note, here’s another method if you’d just want to print the value of an integer inside a string, and that is using string formatting:

> var x int8 = 127

> s := fmt.Sprintf(“%d”, x)

> s

— Arkaprabha Majumdar

16.08.2020

--

--

ArkaprabhaM

Originally from Bhutan. I’m a postgraduate in Data Science. I have a Mathematics+CS background. I have interned in TCS as a Data Science — BA intern for 4 m