filepath.Abs() Function in Golang With Examples

In Go language, path package used for paths separated by forwarding slashes, such as the paths in URLs. The filepath.Abs() function in Go language used to return an absolute representation of the specified path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. Moreover, this function is defined under the path package. Here, you need to import the “path/filepath” package in order to use these functions.

Syntax:

func Abs(path string) (string, error)

Here, ‘path’ is the specified path.

Return Value: It return an absolute representation of the specified path.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// filepath.Abs() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Abs() function to get
    // absolute representation of the specified path
    fmt.Println(filepath.Abs("/home/gfg"))
    fmt.Println(filepath.Abs(".gfg"))
    fmt.Println(filepath.Abs("/gfg"))
    fmt.Println(filepath.Abs(":gfg"))
}

chevron_right


Output:

/home/gfg <nil>
/.gfg <nil>
/gfg <nil>
/:gfg <nil>

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// filepath.Abs() function
  
// Including the main package
package main
  
// Importing fmt and path/filepath
import (
    "fmt"
    "path/filepath"
)
  
// Calling main
func main() {
  
    // Calling the Abs() function to get
    // absolute representation of the specified path
    fmt.Println(filepath.Abs("/"))
    fmt.Println(filepath.Abs("."))
    fmt.Println(filepath.Abs(":"))
    fmt.Println(filepath.Abs("/."))
    fmt.Println(filepath.Abs("//"))
    fmt.Println(filepath.Abs(":/"))
}

chevron_right


Output:

/ <nil>
/ <nil>
/: <nil>
/ <nil>
/ <nil>
/: <nil>



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.