Skip to content

Instantly share code, notes, and snippets.

@johnwesonga
Last active December 14, 2015 20:08
Show Gist options
  • Save johnwesonga/5141298 to your computer and use it in GitHub Desktop.
Save johnwesonga/5141298 to your computer and use it in GitHub Desktop.
Python vs Go on string manipulation
In Python:
key = /cns/pb-d/home/engedu/text/shakespeare/hamlet:0000000000014fb9'
s = key.split("/")[-1]
parts = s.split(":")
print "%s:%s" % (parts[0], str(int(parts[1], 16)))
In Go:
import("fmt"
"strings"
"strconv"
)
key := "/cns/pb-d/home/engedu/text/shakespeare/hamlet:0000000000014fb9"
l := key[1+strings.LastIndex(key, "/"):]
parts := strings.SplitN(l, ":", 2)
num, err := strconv.ParseInt(parts[1], 16, 64)
if err != nil {
fmt.Errorf("bad line number %q: %v", parts[1], err)
}
fmt.Printf("%v:%d", parts[0], num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment