I Spy Code - GO

Get volume name from path

Question:

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

Answer:

The function filepath.VolumeName() returns the leading volume name.

Here is a golang example that gets the drive letter from a path:

Source: (example.go)

package main
 
import (
   "fmt"
   "path/filepath"
)
 
func main() {
 
   mypath := "C:/examples/golang/example.go"
   fmt.Println("Volume:",filepath.VolumeName(mypath))  
}
 

Output:

$ go run example.go
Volume: C:

References:

https://golang.org/pkg/path/filepath/#VolumeName

Questions answered by this page:

How to get the drive letter from a path in golang?
Getting the volume name of a path in golang.