Golang : Convert octal value to string to deal with leading zero problem
Problem :
You have some integer variables or input data that starts with zero and they are causing some funky bugs when displayed on screen in your program. This is known as the leading zero problem and plagued other programming languages as well. Not unique to Golang alone and the developers behind Golang won't fix this issue.
So how to fix this leading zero problem ?
Solution :
At this moment, reflect package still unable to detect if an integer variable is octal or strings.HasPrefix()
can be used to check the integer starts with zero.( only works on string).
Therefore, to be safe, always convert the integer variables to string before displaying the values.
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
var octalInt = 0012
// don't use this method as the leading zero will disappear!
zip := strconv.FormatInt(int64(octalInt), 8) // base 8 for Octal
fmt.Println("Wrong! Don't use this method because of : ")
fmt.Println("Missing zero : ", zip)
fmt.Printf("Type : %v \n", reflect.TypeOf(zip))
fmt.Println("=========================================")
// calculate the size of the octal - the wrong way!
t := reflect.TypeOf(octalInt)
fmt.Println("Type : ", t)
// play.golang.org will return 4
// go run will return 8 ... so not to be trusted for now
fmt.Println("Size : ", t.Size())
fmt.Println("However, we have to count by hand on the actual size of the octalInt")
str := fmt.Sprintf("00%o", octalInt) //pad zero up to the length of 4, o is for expecting octal value
// ** NOTE : Have to hardcode 4 because there is no way I can programmatically insert octalInt's length into
// fmt.Sprintf()
fmt.Println("Converted from integer(octal) to string : ", str)
fmt.Printf("Type : %v \n", reflect.TypeOf(str))
}
Output :
Wrong! Don't use this method because of :
Missing zero : 12
Type : string
=========================================
Type : int
Size : 8
However, we have to count by hand on the actual size of the octalInt
Converted from integer(octal) to string : 0012
Type : string
See also : Golang : Dealing with postal or zip code example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+15.9k Golang : How to reverse elements order in map ?
+7.3k Golang : Gorrila set route name and get the current route name
+25.2k Golang : Convert long hexadecimal with strconv.ParseUint example
+9.3k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+4.5k JavaScript : Rounding number to decimal formats to display currency
+10.4k Golang : Create matrix with Gonum Matrix package example
+29.7k Golang : Get and Set User-Agent examples
+22.8k Golang : Calculate time different
+31.3k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+5.5k Unix/Linux : How to find out the hard disk size?
+7.3k Android Studio : How to detect camera, activate and capture example
+10.1k Golang : How to profile or log time spend on execution?