Skip to content

Instantly share code, notes, and snippets.

@pdk
Created August 30, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdk/4edcf95359f4b4ce13574c411e58247c to your computer and use it in GitHub Desktop.
Save pdk/4edcf95359f4b4ce13574c411e58247c to your computer and use it in GitHub Desktop.
naive refactor of non-error checking in go
// GatherItems collects incoming items. If somehow we get more than n, an error is returned.
func GatherItems(n int) ([]string, error) {
checkLen := func() {
if len(items) > n {
return items, fmt.Errorf("more than %d items gathered: %d", n, len(items))
}
}
var items []string
items = append(items, GetItemsFromSourceAlpha()...)
checkLen()
items = append(items, GetItemsFromSourceBeta()...)
checkLen()
items = append(items, GetItemsFromSourceGamma()...)
checkLen()
return items, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment