Skip to content

Instantly share code, notes, and snippets.

@sent-hil sent-hil/go_style.txt
Last active Dec 17, 2015

Embed
What would you like to do?
1. Comments prefixed with // & space.
// right
//wrong
2. No blank lines between for & next line.
for _, name := range notAllowedSuffixes {
if strings.HasSuffix(sourceName, name) || strings.HasSuffix(targetName, name) {
}
}
3. If last line is return, add blank line above.
do_something()
result := do_something_else()
return result
}
4. Declare `var` right above they're used, not at top of function,
unless in global scope.
5. No blank lines between }}.
}
}
6. Group code together by chunks, if more than 1 line.
// hard to read
relationshipsURL := nodeURL + "/relationships/all"
response := sendRequest("GET", relationshipsURL, "")
relations, err := jsonArrayDecode(response)
7. Error checking if statements shouldn't have else.
// leads to node style callback staircase
if err != nil {
fmt.Println(err)
} else {
neo4j.UpdateNode(sourceId, sourceContent)
}
8. Fail as loudly as possible, log unexcepted data.
if sourceName == "" {
// log here
}
9. Wrap at 80, no less than 60; second & more lines should have more
indentation.
return fmt.Sprintf(
`{"key":"id","value":"%s","properties":{"id":"%s","name":"%s"}}`,
id, id, name)
10. Avoid creating strings with +, use fmt.Sprintf()
url := "amqp://" + user + ":" + password + "@" + host + ":" + port
11. Anonymous return values.
// right
func say(word string) string {}
// wrong
func say(word string) (greeting string) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.