diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index fd54e2d35d..13c2895161 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -12,8 +12,11 @@ Code formatting and naming conventions affect coding readability and maintainabi But having tools to format source code doesn't mean you do not need to care the formatting of the code, for example: +
Bad | Good |
---|---|
+ ```go -// bad badStr := "apiVersion: v1\n" + "kind: Service\n" + "metadata:\n" + @@ -24,8 +27,11 @@ badStr := "apiVersion: v1\n" + " port: 3000\n" + " targetPort: 3000\n" + " protocol: TCP\n" +``` -// good + | + +```go goodStr := `apiVersion: v1 kind: Service metadata: @@ -39,6 +45,8 @@ spec: ` ``` + |
Bad | Good |
---|---|
+ +```go +type S struct {} + +func (s *S) fn() {} + +type I interface {} +``` + + | + +```go +type I interface {} + +type S struct {} + +func (s *S) fn() {} +``` + + |
Bad | Good |
---|---|
+ +```go +type ( + I interface {} + I2 interface {} + + s struct {} + s2 struct {} +) +``` + + | + +```go +type ( + I interface {} + I2 interface {} +) + +type ( + s struct {} + s2 struct {} +) +``` + + |
Bad | Good |
---|---|
+ ```go -// good -if err := fn(); err != nil { +err := fn() +if err != nil { // handle error } ``` -Instead of this style. + | ```go -// bad -err := fn() -if err != nil { +if err := fn(); err != nil { // handle error } ``` + |
Bad | Good |
---|---|
+ ```go -// good -conn, err := net.Dial("tcp", "localhost:80") -if err != nil { +if conn, err := net.Dial("tcp", "localhost:80"); err != nil { // handle error +} else { + // use the conn } - -// use the conn ``` -Instead of this style. + | ```go -// bad -if conn, err := net.Dial("tcp", "localhost:80"); err != nil { +conn, err := net.Dial("tcp", "localhost:80") +if err != nil { // handle error -} else { - // use the conn } +// use the conn ``` + |