Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func dot_git_dir(dir string) string {
git_dir := path.Join(dir, ".git")
_, dir_error := os.Stat(git_dir)
if dir_error != nil {
if git_dir == "/.git" {
log.Fatal("Cannot find a .git directory")
}
return dot_git_dir(path.Dir(dir))
} else {
return git_dir
}
}
@dmitshur

This comment has been minimized.

Copy link

@dmitshur dmitshur commented Nov 8, 2014

It looks like you're dealing with file paths here, so the correct package to use would be path/filepath, not path. That way it's more readable and has a better chance of working on all 3 OSes.

Using path instead of path/filepath feels like seeing someone use text/template instead of html/template to generate HTML... It may work for simple cases, but it's better to use the correct package and be safe.

@anisse

This comment has been minimized.

Copy link

@anisse anisse commented Nov 8, 2014

Idiomatic golang uses the early return pattern, which means you can eliminate the else part of your code. See:
https://golang.org/doc/effective_go.html#if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment