From 8ed6b29fe0c005e3da7fcb41ebc5758af17f5c50 Mon Sep 17 00:00:00 2001 From: Dong Liang Date: Thu, 13 Jul 2017 16:38:18 +1200 Subject: [PATCH] Add Golang Version --- Go/ShortURL.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Go/ShortURL.go diff --git a/Go/ShortURL.go b/Go/ShortURL.go new file mode 100644 index 0000000..e4074e4 --- /dev/null +++ b/Go/ShortURL.go @@ -0,0 +1,46 @@ +/* + * ShortURL (https://github.com/delight-im/ShortURL) + * Copyright (c) delight.im (https://www.delight.im/) + * Licensed under the MIT License (https://opensource.org/licenses/MIT) + */ + +/** + * shorturl: Bijective conversion between natural numbers (IDs) and short strings + * + * shorturl.Encode() takes an ID and turns it into a short string + * shorturl.Decode() takes a short string and turns it into an ID + * + * Features: + * + large alphabet (51 chars) and thus very short resulting strings + * + proof against offensive words (removed 'a', 'e', 'i', 'o' and 'u') + * + unambiguous (removed 'I', 'l', '1', 'O' and '0') + * + * Example output: + * 123456789 <=> pgK8p + */ + +package shorturl + +import ( + "strings" +) + +const Alphabet string = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ-_" +const Base int = len(Alphabet) + +func Encode(num int) string { + bytes := []byte{} + for num > 0 { + bytes = append([]byte{Alphabet[num%Base]}, bytes...) + num = num / Base + } + return string(bytes) +} + +func Decode(str string) int { + num := 0 + for i := 0; i < len(str); i++ { + num = num*Base + strings.Index(Alphabet, string(str[i])) + } + return num +}