I have this code and I don't like the way it feels not to mention golint
doesn't like it with error should be the last type when returning multiple items (golint)
. To explain this code:
I am open to refactoring this in any way (be it breaking it apart, moving things around, etc.) Is there a more idiomatic way in Go to accomplish something like this?
// Download will download the audio and video files to a particular path
func (r *RedditVideo) Download() (outputVideoFileName string, outputAudioFileName string, errVideo error, errAudio error) {
outputVideoFile, errVideo := downloadTemporaryFile(
r.VideoURL,
TemporaryVideoFilePrefix,
)
if errVideo == nil {
outputVideoFileName = outputVideoFile.Name()
}
outputAudioFile, errAudio := downloadTemporaryFile(
r.AudioURL,
TemporaryAudioFilePrefix,
)
if errAudio == nil {
outputAudioFileName = outputAudioFile.Name()
}
return outputVideoFileName, outputAudioFileName, errVideo, errAudio
}
Similarly I found myself using this same pattern again later in code:
// SetFiles sets up all of the input files and the output file
func (l *localVideo) SetFiles(inputVideoFilePath string, inputAudioFilePath string, outputVideoFilePath string) (errVideo error, errAudio error, errOutputVideo error) {
// Set input video file
l.inputVideoFile, errVideo = os.Open(inputVideoFilePath)
if errVideo != nil {
return
}
if errVideo = l.inputVideoFile.Close(); errVideo != nil {
return
}
// Set output video file
l.outputVideoFile, errOutputVideo = os.Create(outputVideoFilePath)
if errOutputVideo != nil {
return
}
if errOutputVideo = l.outputVideoFile.Close(); errOutputVideo != nil {
return
}
// IMPORTANT! Set input audio file LAST incase it is nil
l.inputAudioFile, errAudio = os.Open(inputAudioFilePath)
if errAudio != nil {
return
}
if errAudio = l.inputAudioFile.Close(); errVideo != nil {
return
}
return
}
This time in this code again some of the same is true like above:
</div>
What you can see quite a bit in the standard library are specific functions which wrap more generic non-exported functions. See the commented code below.
// download is a rather generic function
// and probably you can refactor downloadTemporaryFile
// so that even this function is not needed any more.
func (r *RedditVideo) download(prefix, url string) (filename string, error error) {
outputFile, err := downloadTemporaryFile(
r.VideoURL,
prefix,
)
if err == nil {
return "", fmt.Errorf("Error while download: %s", err)
}
return outputFile.Name(), nil
}
// DownloadVideo wraps download, handing over the values specific
// to the video download
func (r *RedditVideo) DownloadVideo() (filename string, averror error) {
return r.download(TemporaryVideoFilePrefix, r.VideoURL)
}
// DownloadAudio wraps download, handing over the values specific
// to the audio download
func (r *RedditVideo) DownloadAudio() (filename string, averror error) {
return r.download(TemporaryAudioFilePrefix, r.AudioURL)
}
func main() {
r := RedditVideo{
VideoURL: os.Args[1],
AudioURL: os.Args[2],
}
var videoerror, audioerror error
var videofileName, audiofileName string
if videofileName, videoerror = r.DownloadVideo(); videoerror != nil {
fmt.Println("Got an error downloading the video")
}
if audiofileName, audioerror = r.DownloadAudio(); audioerror != nil {
fmt.Println("Got an error downloading the audio")
}
fmt.Printf("Video:\t%s
Audio:\t%s", videofileName, audiofileName)
}
This way, it is obvious from which download the returned error stems from.
From https://blog.golang.org/error-handling-and-go:
Go code uses error values to indicate an abnormal state.
In your situation, Audio is optional and Video is required. Therefore only Video download should return error:
// Download will download the audio and video files to a particular path
// empty outputAudioFileName indicates inability to download Audio File
func (r *RedditVideo) Download() (outputVideoFileName string, outputAudioFileName string, err error) {
outputVideoFile, err := downloadTemporaryFile(
r.VideoURL,
TemporaryVideoFilePrefix,
)
if err != nil {
return "", "", err
}
outputVideoFileName := outputVideoFile.Name()
outputAudioFile, errAudio := downloadTemporaryFile(
r.AudioURL,
TemporaryAudioFilePrefix,
)
outputAudioFileName := ""
if errAudio == nil {
outputAudioFileName = outputAudioFile.Name()
} else {
// Log error, it is still important to fix it
}
return outputVideoFileName, outputAudioFileName, nil
}
Rule of thumb - any code that can generate abnormal state should have in next line:
if err != nil {
return funcResult, err
}