I Spy Code - GO

Split path os path separator

Question:

How to split your path into a list of individual paths in golang?

Answer:

The function filepath.SplitList() splits a list of paths joined by the OS specific path separator.

Here is a golang example that shows how to split your path into a list of individual paths (array of strings):

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
 
func main() {
   mypath := "/usr/local/go/bin:/usr/local/bin:/usr/bin:/usr/sbin"
   paths := filepath.SplitList(mypath)
   for i := range paths {
      fmt.Println(paths[i])
   }
}
 

Output:

$ go run example.go
/usr/local/go/bin
/usr/local/bin
/usr/bin
/usr/sbin

References:

https://golang.org/pkg/path/filepath/#SplitList