I Spy Code - GO

Last element of path

Question:

How do the last element of a path in golang?

Answer:

The function filepath.Base() returns the last element of path.

Here is a golang example that shows how to get the base (last element) of a path:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main() {
   base := filepath.Base("/home/dennis/IdeaProjects/Playground/hello.go")
   fmt.Println("Base:", base)
}
 

Output:

$ go run example.go
Base: hello.go

References:

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