My string splitting skills are really poor using Go. I don't know why but I just can't wrap my head around this one and I've tried different ways to do what I need but I always get the wrong output.
I have this string:
https://docs.google.com/spreadsheets/d/1k2RNDSh6K7jH3fezjjfgVrHqVstBF569al4F28hqAM/edit#gid=0
I need to get a key from this URL that is found after '/d/' up until the next '/'.
So the key in this case would be:
1k2RNDSh6K7jH3fezjjfgVrHqVstBF569al4F28hqAM
How would I go about doing this?
strings.Index
can help you with this:
s := "https://docs.google.com/spreadsheets/d/1k2RNDSh6K7jH3fezjjfgVrHqVstBF569al4F28hqAM/edit#gid=0"
i := strings.Index(s, "/d/") + len("/d/")
j := strings.Index(s[i:], "/") + i
fmt.Println(s[i:j]) // Prints "1k2RNDSh6K7jH3fezjjfgVrHqVstBF569al4F28hqAM"
Playground: http://play.golang.org/p/Mqnnc_tNFk.
I have never used "go" but it looks like you could utilize
func Split(s, set string) []string
Example:
fmt.Printf("%q
", strings.Split("https://docs.google.com/spreadsheets/d/1234567890/edit#gid...", "/"))
would return the array
["https:" "" "docs.google.com" "spreadsheets" "d" "1234567890" "edit#gid..."]
There are many alternatives to do this, here is the one which I used regular expression.
s := "https://docs.google.com/spreadsheets/d/1k2RNDSh6K7jH3fezjjfgVrHqVstBF569al4F28hqAM/edit#gid=0"
re := regexp.MustCompile("/d/(.*)?/").FindAllStringSubmatch(s, -1)
fmt.Println("found the pattern :", re[0][1])