Golang : Dealing with postal or zip code example
Problem :
For some countries, the postal code
or zip code
starts with zero and this can be problematic for developers. Quite frequently, programmers will assume that the postal or zip code is in integer type and this is will introduce funky bugs later on. Why? because leading zero in integer will the turn the variable/constant value to become Octal value and thus screw up the final value.
Solution :
The code below will simulate a zip code input with leading zero and type integer. It will demonstrate the problem and show you how to fix it.
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
//leading zero will convert zip code to octal!
var iZip = 0000123
fmt.Println("No longer int, but octal value : ", iZip) //funky value
fmt.Printf("Type : %v \n", reflect.TypeOf(iZip))
fmt.Println("=========================================")
// do not use this method as the leading zero will disappear!
zip := strconv.FormatInt(int64(iZip), 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("=========================================")
// always convert zip code to type string before display!
// calculate the size of the iZip octal - the wrong way!
t := reflect.TypeOf(iZip)
fmt.Println("Type : ", t)
// play.golang.org return 4
// go run will return 8 bytes - not to be trusted for now
fmt.Println("Size : ", t.Size())
// pad zero up to the length of 7, o is for expecting octal value
newZip := fmt.Sprintf("0000%o", iZip)
// ** NOTE : Have to hardcode 7 because there is no way
// I can programmatically insert the t.Size()-1 into fmt.Sprintf()
fmt.Println("Converted from integer to string : ", newZip)
fmt.Printf("Type : %v \n", reflect.TypeOf(newZip))
}
Output :
No longer int, but octal value : 83
Type : int
=========================================
Wrong! Don't use this method because of :
Missing zero : 123
Type : string
=========================================
Converted from integer to string : 0000123
Type : string
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
+6.5k Golang : Output or print out JSON stream/encoded data
+5.5k Linux/Unix/PHP : Restart PHP-FPM
+4.4k MariaDB/MySQL : How to get version information
+25.4k Golang : missing Mercurial command
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.1k Golang : How to protect your source code from client, hosting company or hacker?
+17k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+27.1k PHP : Convert(cast) string to bigInt
+20.8k Golang : Sort and reverse sort a slice of strings
+7.9k Golang : Add build version and other information in executables
+6.2k Grep : How to grep for strings inside binary data
+10.6k Golang : Command line file upload program to server example