I am splitting file names in Go to get at the file extension (e.g. import ("strings") ; strings.Split("example.txt", ".")
). For this reason, I would like to return the last item in the slice returned by the split, i.e.
for strings.Split("ex.txt", "."), I want txt
This question suggests that doing
strings.Split("ex.txt", ".")[len(strings.Split("ex.txt", ".")) - 1]
is the only way to get at it. That is, there is no -1
as in Python. This seems very wasteful to me, as I feel we are doing the same splitting operation twice.
Split
into a variable, or just do the above?Thank you!
You should assign the results of the split to a variable, instead of calling it twice.
ss := strings.Split(msg, ".")
s := ss[len(ss)-1]
(Notice that this allows (or maybe forces) you to deal with the case where ss
is empty or something else unexpected explicitly, before indexing it.)
If you're doing this over and over again and it offends you having to use two lines (or two lines plus error handling) instead of one you can abstract it into a function pretty easily:
func lastString(ss []string) string {
return ss[len(s)-1]
}
s1 := lastString(strings.Split("example.txt", "."))
s2 := lastString(strings.Split("example.jpg", "."))
After all, passing the result of a function as an argument has essentially the same effect as binding it to a variable.
I would use the filepath package rather than parsing it yourself:
import "path/filepath"
s := "file.ext"
ext := filepath.Ext(s)
if len(ext) != 0 {
fmt.Printf("ext = %s
", ext[1:])
} else {
fmt.Printf("no ext
")
}
This will work with a full path as well as just a file name.
strings.LastIndex
makes this quite neat:
s := "Hello,Stack,Overflow"
last := s[strings.LastIndex(s, ",")+1:]
fmt.Println(last)
returns "Overflow". If the search string isn't found it returns the whole string, which is logical.
Playground here