Golang: convert rune to int

If you want to convert a rune into an int with go you can try to use int(rune), but doing it will not return the expected result. Since you want the integer value of the ASCII (or UTF-8) representation of the number you want to convert.

For example suppose you want to convert the rune with the value 56 to an int, doing int(rune) will return 56. Buy 56 is the code of the integer 8.

ASCII table for integers

To solve this problem you will need to substract 48 to 56 and you will get the 8. Why 48? Well 48 is the representation of the "0" in the ascii table and since all the integers are sequential in the ASCII representation, this will return the desired value.

int(r - '0')