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:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Abs( "/home/gfg" ))
fmt.Println(filepath.Abs( ".gfg" ))
fmt.Println(filepath.Abs( "/gfg" ))
fmt.Println(filepath.Abs( ":gfg" ))
}
|
Output:
/home/gfg <nil>
/.gfg <nil>
/gfg <nil>
/:gfg <nil>
Example 2:
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(filepath.Abs( "/" ))
fmt.Println(filepath.Abs( "." ))
fmt.Println(filepath.Abs( ":" ))
fmt.Println(filepath.Abs( "/." ))
fmt.Println(filepath.Abs( "//" ))
fmt.Println(filepath.Abs( ":/" ))
}
|
Output:
/ <nil>
/ <nil>
/: <nil>
/ <nil>
/ <nil>
/: <nil>
Last Updated :
10 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...