Created
November 7, 2014 17:51
-
-
Save adamhjk/5a475b8dd45971a4e814 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
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
It looks like you're dealing with file paths here, so the correct package to use would be
path/filepath, notpath. That way it's more readable and has a better chance of working on all 3 OSes.Using
pathinstead ofpath/filepathfeels like seeing someone usetext/templateinstead ofhtml/templateto generate HTML... It may work for simple cases, but it's better to use the correct package and be safe.