I Spy Code - GO

Is path absolute

Question:

How do tell if a path is an absolute path in golang?

Answer:

The function filepath.IsAbs() reports whether the path is absolute.

Here is a golang example that shows how to test is a path is an absolute path:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main() {
   abs := filepath.IsAbs("../../Playground/hello.go")
   fmt.Println("Is absolute:", abs)
}
 

Output:

$ go run example.go
Is absolute: false

References:

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