ADVERTISEMENT
ADVERTISEMENT

How to get the volume name from a path in Golang?

Given a path, we have to find the volume name from the path in Golang.
Submitted by IncludeHelp, on October 28, 2021

In the Go programming language, to get the volume name from the given path – we use the VolumeName() function of path/filepath package. The VolumeName() function returns the leading volume name.

For Example, given "C:\foo\bar" it returns "C:" on Windows. Given "\\host\share\foo" it returns "\\host\share". On other platforms it returns "".

Syntax:

func VolumeName(path string) string

Consider the below Golang program demonstrating how to get the volume name from a path?

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	// Defining a path
	path := "C:/programs/course1/hello1.go"

	// Calling function VolumeName() to
	// get the volume name
	volume_name := filepath.VolumeName(path)

	// Printig the volume name
	fmt.Println("The Volume Name is: ", volume_name)
}

Output

The Volume Name is: C:

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.