Problem overview
The function fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. The name has at least one dot and neither starts nor ends with one. Names use only English letters, digits, dots, - and _.
For "photo.jpeg" the extension is "jpeg". A name can have several dots: "archive.tar.gz" gives "gz", because only what follows the last dot counts. The extension keeps its case, so "README.MD" gives "MD".
Approach
Cut the name at every dot, and take the last piece. strings.Split(s, sep) does the cutting: it returns a slice of the pieces of s that lie between the separators. For "archive.tar.gz" and the separator ".", the pieces are archive, tar and gz.
It doesn't matter how many dots the name has. The last piece always comes after the last dot, which is the extension.
Step by step
- Split the name into a slice named
partswithstrings.Split(fileName, "."). The separator is a string, so it is written in double quotes. - Return the last item. A slice is numbered from 0 and
lenworks on it, so the last item is at indexlen(parts)-1.
The solution in Go
func fileExtension(fileName string) string {
parts := strings.Split(fileName, ".")
return parts[len(parts)-1]
}
Running it on the three examples gives these results:
"photo.jpeg" -> "jpeg"
"archive.tar.gz" -> "gz"
"README.MD" -> "MD"
For "archive.tar.gz" the slice parts prints as [archive tar gz], and its length is 3, so the code reads the item at index 2.
Details worth knowing:
- The result is a slice of strings.
strings.Splitreturns a[]string, soparts[2]is astring. No conversion is needed before returning it. - Go has no negative indexes. The last item is
parts[len(parts)-1]; there is noparts[-1], and writing it as a constant index doesn't compile. - The separator isn't kept. The dots disappear from the pieces, so the extension comes back without its leading dot.
- The
stringspackage. In a file of your own, addimport "strings". In the lesson's editor it is already there.
Another way: strings.LastIndex
The split creates a piece for every part of the name, although only the last one is wanted. strings.LastIndex(s, sub) returns the index of the last occurrence of sub, so the extension is everything after it:
name := "archive.tar.gz"
ext := name[strings.LastIndex(name, ".")+1:]
For "archive.tar.gz" the last dot is at index 11, so the slice starts at 12 and ext is "gz". Both versions give the same answers for this lesson's names. The lesson's steps use strings.Split, which also works for the other kind of task, cutting a string into all of its parts.
Edge cases and common mistakes
- Taking the second piece.
parts[1]works for"photo.jpeg"but returns"tar"for"archive.tar.gz". The last piece is the one that is always right. - Using
len(parts)as the index. The last valid index is one less. Indexing atlen(parts)stops the program with an "index out of range" panic. - A name without a dot. The constraints rule it out. For
"noext",strings.Splitreturns a slice with the whole name as its only piece, so the function would return the name itself instead of an empty extension. - A trailing dot. A name that ends with a dot would give an empty last piece. The constraints exclude that too.
- Changing the case. Keep the letters as they are:
"MD"stays"MD".
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
