← All languages
Go function of the day
Random

filepath.Abs

Return the absolute path of a file.

Description

filepath.Abs returns an absolute representation of path. If the path is not absolute, it is joined with the current working directory to turn it into an absolute path.

The returned path is cleaned as if by filepath.Clean. filepath.Abs calls os.Getwd to obtain the current directory if needed.

filepath.Abs is commonly used to normalize relative paths into absolute paths for reliable file operations.

Arguments

NameDescriptionOptional
path The file path to convert to absolute. No

Example

abs, err := filepath.Abs("file.txt")
if err != nil {
    log.Fatal(err)
}
fmt.Println(abs)  // /home/user/project/file.txt

Reference