To find the name of a directory in Go, you can use the filepath
and strings
packages.
First, use the filepath.Split()
function to split the file path into its directory and file name components:
main.go202 chars13 lines
Then, you can use the strings.TrimSuffix()
function to remove the trailing slash and get just the directory name:
main.go254 chars15 lines
If you need to handle the case where the path ends with a backslash on Windows, you can use the path/filepath
package's VolumeName
and IsPathSeparator
functions to deal with that edge case.
gistlibby LogSnag