Standard library / path/filepath

path/filepath.Abs()

Returns a cleaned absolute OS path, using the current directory when necessary; not necessarily a unique pathname. Example uses a disposable temporary workspace. Expected stdout: 'true docs\n'.

Formal Syntax & Structure

go syntaxgo
func Abs(path string) (string, error)

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.

Return Value

(string, error)

Returns a cleaned absolute OS path, using the current directory when necessary; not necessarily a unique pathname.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  p, e := filepath.Abs("docs")  check(e)  fmt.Println(filepath.IsAbs(p), filepath.Base(p))}
Go Reference