I have trivial question.
I have string which contains a filename and it's path. How can i remove whole path? I have tried those:
line = "/some/path/to/remove/file.name" line := strings.LastIndex(line, "/") fmt.Println(line)
It prints some strange number:
38
I need it without last slash
Thanks a lot
split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.
The set of names required to specify a particular file in a hierarchy of directories is called the path to the file, which you specify as a path name.
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base
:
path := "/some/path/to/remove/file.name" file := filepath.Base(path) fmt.Println(file)
Playground: http://play.golang.org/p/DzlCV-HC-r.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With