diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2020-03-23 17:41:03 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2020-03-23 17:41:03 +0300 |
commit | 7ae3fb8a745949f27d2580e039c0a2f8ffe029a3 (patch) | |
tree | 653b4a3fbc33e264637febcf3d38a2b7f85049ff | |
parent | 838937325bc4c2cfd0e99dd1fc463dd703770b56 (diff) | |
parent | 556019ef49dcf72257133ab744f0d64081e5595e (diff) |
Merge branch 'jv-style-things' into 'master'
Add style notes about pointer constructors and byte literals
See merge request gitlab-org/gitaly!1950
-rw-r--r-- | STYLE.md | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -41,6 +41,43 @@ interpolating strings. The `%q` operator quotes strings and escapes spaces and non-printable characters. This can save a lot of debugging time. +## Literals and constructors + +### Use "address of struct" instead of new + +The following are equivalent in Go: + +```golang +// Preferred +foo := &Foo{} + +// Don't use +foo := new(Foo) +``` + +There is no strong reason to prefer one over the other. But mixing +them is unnecessary. We prefer the first style. + +### Use hexadecimal byte literals in strings + +Sometimes you want to use a byte literal in a Go string. Use a +hexadecimal literal in that case. Unless you have a good reason to use +octal of course. + +```golang +// Preferred +foo := "bar\x00baz" + +// Don't use octal +foo := "bar\000baz" +``` + +Octal has the bad property that to represent high bytes, you need 3 +digits, but then you may not use a `4` as the first digit. 0377 equals +255 which is a valid byte value. 0400 equals 256 which is not. With +hexadecimal you cannot make this mistake because the largest two digit +hex number is 0xff which equals 255. + ## Return statements ### Don't use "naked return" |