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.
- Is there no better command for getting the last item of a slice in Go?
- If no, would the best approach be to write the result of
Split
into a variable, or just do the above?
Thank you!