Skip to content

Fix spacing in the go style code blocks #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 71 additions & 71 deletions docs/developer/go-style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,32 @@ DO:

```go
func longFunctionDefinition(
paramX int,
paramY string,
paramZ bool,
paramX int,
paramY string,
paramZ bool,
) (string, error){}

// and

s := myStruct{
field1: 1,
field2: 2,
field3: 3,
field1: 1,
field2: 2,
field3: 3,
}
```

DO NOT:

```go
func longFunctionDefinition(paramX int, paramY string,
paramZ bool,
paramZ bool,
) (string, error){}

// or

func longFunctionDefinition(
paramX int, paramY string,
paramZ bool,
paramX int, paramY string,
paramZ bool,
) (string, error){}

// or
Expand All @@ -92,12 +92,12 @@ Example:

```go
cfg := foo.Config{
Site: "example.com",
Out: os.Stdout,
Dest: c.KeyPair{
Key: "style",
Value: "well formatted",
},
Site: "example.com",
Out: os.Stdout,
Dest: c.KeyPair{
Key: "style",
Value: "well formatted",
},
}
```

Expand Down Expand Up @@ -176,7 +176,7 @@ DO NOT:

```go
func(int required, int optional) {
if optional {...}
if optional {...}
}
```

Expand All @@ -186,15 +186,15 @@ DO:
type Option func (o *Object)

func Optional(string optional) Option {
return func (o *Object) {
o.optional = optional
}
return func (o *Object) {
o.optional = optional
}
}

func (int required, ...Options) {
for o := range Options {
o(self)
}
for o := range Options {
o(self)
}
}
```

Expand All @@ -217,22 +217,22 @@ DO NOT:

```go
func badAtStuff(noData string) error {
if len(noData) == 0 {
fmt.Printf("Received no data")
}
if len(noData) == 0 {
fmt.Printf("Received no data")
}

return errors.New("received no data")
return errors.New("received no data")
}
```

DO

```go
func badAtStuff(noData string) error {
if len(noData) == 0 {
return errors.New("received no data")
}
...
if len(noData) == 0 {
return errors.New("received no data")
}
...
}
```

Expand All @@ -253,21 +253,21 @@ func onError(err error) {
}

func ReadAsync(r io.Reader, onError) {
err := r()
if err != nil {
onError(err)
}
err := r()
if err != nil {
onError(err)
}
}

go ReadAsync(reader, onError)

// OR errs := make(chan error)

func ReadAsync(r io.Reader, errs chan<- error) {
err := r()
if err != nil {
// put error on errs channel, but don't block forever.
}
err := r()
if err != nil {
// put error on errs channel, but don't block forever.
}
}
```

Expand All @@ -281,35 +281,35 @@ Example:

```go
func readFile(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

return data, nil
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return data, nil
}

func processFile(filename string) error {
data, err := readFile(filename)
if err != nil {
return fmt.Errorf("failed to process file: %w", err)
}
// Process the file data here
return nil
data, err := readFile(filename)
if err != nil {
return fmt.Errorf("failed to process file: %w", err)
}
// Process the file data here
return nil
}

func main() {
filename := "example.txt"
err := processFile(filename)
if err != nil {
fmt.Printf("Error processing file: %v\n", err) // caller handles the error
}
filename := "example.txt"
err := processFile(filename)
if err != nil {
fmt.Printf("Error processing file: %v\n", err) // caller handles the error
}
}
```

Expand Down Expand Up @@ -413,27 +413,27 @@ FAVOR:

```go
type Object struct{
subobject SubObject
subobject SubObject
}

func New() Object {
return Object{
subobject: SubObject{},
}
return Object{
subobject: SubObject{},
}
}
```

DISFAVOR:

```go
type Object struct{
subobject *SubObject
subobject *SubObject
}

func New() *Object {
return &Object{
subobject: &SubObject{},
}
return &Object{
subobject: &SubObject{},
}
}
```

Expand All @@ -455,8 +455,8 @@ DO NOT:

```go
func(s string) *string {
s := s + "more strings"
return &s // this will move to heap
s := s + "more strings"
return &s // this will move to heap
}
```

Expand Down