Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavlo Strokov <pstrokov@gitlab.com>2020-03-23 17:41:03 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2020-03-23 17:41:03 +0300
commit7ae3fb8a745949f27d2580e039c0a2f8ffe029a3 (patch)
tree653b4a3fbc33e264637febcf3d38a2b7f85049ff
parent838937325bc4c2cfd0e99dd1fc463dd703770b56 (diff)
parent556019ef49dcf72257133ab744f0d64081e5595e (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.md37
1 files changed, 37 insertions, 0 deletions
diff --git a/STYLE.md b/STYLE.md
index bf2dae5dd..fae4e0a98 100644
--- a/STYLE.md
+++ b/STYLE.md
@@ -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"