Start by profiling your program. Profiling will help you know what part of your program is using the most time, memory, etc. You need to know what is happening to know what to improve or even if there are useful improvements you can do. https://blog.golang.org/profiling-go-programs gives an example of how to do so and the thinking required.
I have not profiled your code, but have made some guesses based on the principle that the fastest code is the code that does not exist.
strings.Replace searches the string to find where the extension is. The extension is always at the end, and since the extension is already known (it is stored in ext), newname := path[:len(path)-len(ext)] + *flagExt will give the same result without needing filepath.Dir, filepath.Base, filepath.Join, or strings.Replace.
Also, the code you posted does not do what it indicates it should do. The description for flagPath says it should be searching for “jpg files”, but if ext != "" actually checks for files with extensions.
As mentioned, measure. But I would be quite surprised if the majority of the time was not spent in the os.Rename call - that is, waiting for the filesystem layer.
@nathankerr
That’s quite a lot to process for me. Go is my first “high-level” language
Thanks for providing so much detailed information, it will take a while
to get into all of this.
Changing the newname := …
already trimmed down the processing time by about 2 seconds
“path to traverse in search of jpg files” -> An oversight…
@calmh
That’s probably the issue but I get to this once I know more about profiling…