I Spy Code - GO

Split path into file directory and file

Question:

How to split a path into it directory and file names in go lang?

Answer:

The function path.Split() splits the path immediately following the final slash, separating it into a directory and file name component.

Here is a go lang example that splits a path into its directory and file components:

Source: (example.go)

package main
 
import (
   "fmt"
   "path"
)
 
func main() {
 
   dir, file := path.Split("/Users/dennis/ISPY/GO/example.go")
   fmt.Println("Dir:", dir)
   fmt.Println("File:", file)
}
 

Output:

$ go run example.go
Dir: /Users/dennis/ISPY/GO/
File: example.go

References:

https://golang.org/pkg/path/#Split