ADVERTISEMENT
ADVERTISEMENT

How to check if a path is an absolute path in Golang?

Given a path, we have to check if a path is an absolute path in Golang.
Submitted by IncludeHelp, on October 28, 2021

In the Go programming language, to check whether the given path is an absolute path or not – we use the IsAbs() function of the path/filepath package. The IsAbs() function true if the given path is an absolute path; false, otherwise.

Syntax:

func IsAbs(path string) bool

Consider the below Golang program demonstrating how to check if a path is an absolute path?

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	// Defining paths
	path1 := "/programs/course1/hello1.go"
	path2 := "../programs/course1/hello1.go"
	path3 := "C:/programs/course1/hello1.go"

	// Calling IsAbs() to check the path
	// whether absolute or not
	if filepath.IsAbs(path1) {
		fmt.Println(path1, " is an absolute path.")
	} else {
		fmt.Println(path1, " is not an absolute path.")
	}

	if filepath.IsAbs(path2) {
		fmt.Println(path2, " is an absolute path.")
	} else {
		fmt.Println(path2, " is not an absolute path.")
	}

	if filepath.IsAbs(path3) {
		fmt.Println(path3, " is an absolute path.")
	} else {
		fmt.Println(path3, " is not an absolute path.")
	}
}

Output

/programs/course1/hello1.go  is an absolute path.
../programs/course1/hello1.go  is not an absolute path.
C:/programs/course1/hello1.go  is not an absolute path.

Golang path/filepath Package Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.