ADVERTISEMENT
ADVERTISEMENT

How to split the path into a list of individual paths in Golang?

Given a path, we have to split the path into a list of individual paths.
Submitted by IncludeHelp, on October 27, 2021

In the Go programming language, to split the path into a list of individual paths – we use the SplitList() function of path/filepath package. The SplitList() function splits a list of paths joined by the OS-specific ListSeparator (usually found in PATH or GOPATH environment variables). Unlike strings.Split(), SplitList() returns an empty slice when passed an empty string.

Syntax:

func SplitList(path string) []string

Consider the below Golang program demonstrating how to split the path into a list of individual paths (array of strings)?

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	// Path value
	path := "/usr/local/go/bin:/usr/local/bin:/usr/bin:/usr/sbin"
	result := filepath.SplitList(path)

	// Printing the paths (array of string)
	fmt.Println("Paths (array of string)...")
	fmt.Printf("%q\n\n", result)

	// Printing the list of individual paths
	fmt.Println("List of individual paths...")
	for i := range result {
		fmt.Println(result[i])
	}
}

Output

Paths (array of string)...
["/usr/local/go/bin" "/usr/local/bin" "/usr/bin" "/usr/sbin"]

List of individual paths...
/usr/local/go/bin
/usr/local/bin
/usr/bin
/usr/sbin

Golang path/filepath Package Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

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.