Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
/bin

.DS_Store
.env
.envrc
.idea/
.vscode/
76 changes: 0 additions & 76 deletions CODE_OF_CONDUCT.md

This file was deleted.

28 changes: 5 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
This repository holds the [Uber Go Style Guide](style.md), which documents
patterns and conventions used in Go code at Uber.
This repository holds the [Velmie Go Style Guide](style.md), which documents
patterns and conventions used in Go code at Velmie.

## Style Guide

See [Uber Go Style Guide](style.md) for the style guide.
See [Velmie Go Style Guide](style.md) for the style guide.

## Translations
## Contributing

We are aware of the following translations of this guide by the Go community.

- **中文翻译** (Chinese): [xxjwxc/uber_go_guide_cn](https://github.com/xxjwxc/uber_go_guide_cn)
- **繁體中文** (Traditional Chinese):[ianchen0119/uber_go_guide_tw](https://github.com/ianchen0119/uber_go_guide_tw)
- **한국어 번역** (Korean): [TangoEnSkai/uber-go-style-guide-kr](https://github.com/TangoEnSkai/uber-go-style-guide-kr)
- **日本語訳** (Japanese): [knsh14/uber-style-guide-ja](https://github.com/knsh14/uber-style-guide-ja)
- **Traducción al Español** (Spanish): [friendsofgo/uber-go-guide-es](https://github.com/friendsofgo/uber-go-guide-es)
- **แปลภาษาไทย** (Thai): [pallat/uber-go-style-guide-th](https://github.com/pallat/uber-go-style-guide-th)
- **Tradução em português** (Portuguese): [lucassscaravelli/uber-go-guide-pt](https://github.com/lucassscaravelli/uber-go-guide-pt)
- **Tradução em português** (Portuguese BR): [alcir-junior-caju/uber-go-style-guide-pt-br](https://github.com/alcir-junior-caju/uber-go-style-guide-pt-br)
- **Tłumaczenie polskie** (Polish): [DamianSkrzypczak/uber-go-guide-pl](https://github.com/DamianSkrzypczak/uber-go-guide-pl)
- **Русский перевод** (Russian): [sau00/uber-go-guide-ru](https://github.com/sau00/uber-go-guide-ru)
- **Français** (French): [rm3l/uber-go-style-guide-fr](https://github.com/rm3l/uber-go-style-guide-fr)
- **Türkçe** (Turkish): [ksckaan1/uber-go-style-guide-tr](https://github.com/ksckaan1/uber-go-style-guide-tr)
- **Український переклад** (Ukrainian): [vorobeyme/uber-go-style-guide-uk](https://github.com/vorobeyme/uber-go-style-guide-uk)
- **ترجمه فارسی** (Persian): [jamalkaksouri/uber-go-guide-ir](https://github.com/jamalkaksouri/uber-go-guide-ir)
- **Tiếng việt** (Vietnamese): [nc-minh/uber-go-guide-vi](https://github.com/nc-minh/uber-go-guide-vi)

If you have a translation, feel free to submit a PR adding it to the list.
Make changes according to the [guide](CONTRIBUTING.md) and makes a pull-request. The CI generates the result `style.md` file.
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Uber Go Style Guide
# Go Style Guide

- [Introduction](intro.md)
- Guidelines
Expand All @@ -10,6 +10,7 @@
- [Defer to Clean Up](defer-clean.md)
- [Channel Size is One or None](channel-size.md)
- [Start Enums at One](enum-start.md)
- [String() for enums](enum-string.md)
- [Use `"time"` to handle time](time.md)
- Errors
- [Error Types](error-type.md)
Expand Down Expand Up @@ -63,4 +64,3 @@
- Patterns
- [Test Tables](test-table.md)
- [Functional Options](functional-option.md)
- [Linting](lint.md)
27 changes: 27 additions & 0 deletions src/container-copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,31 @@ snapshot := stats.Snapshot()
</td></tr>
</tbody></table>

## Always return a slice if a function modifies one

<table>
<thead><tr><th>Bad</th> <th>Good</th></tr></thead>
<tbody>
<tr>
<td>

```go
func Add(trips []int) {
trips = append(trips, 1)
}
```

</td>
<td>

```go
func Add(trips []int) []int {
return append(trips, 1)
}
```

</td>
</tr>

</tbody>
</table>
23 changes: 22 additions & 1 deletion src/enum-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
There are cases where using the zero value makes sense, for example when the
zero value case is the desirable default behavior.

Example 1:

```go
type LogOutput int

Expand All @@ -53,4 +55,23 @@ const (
// LogToStdout=0, LogToFile=1, LogToRemote=2
```

<!-- TODO: section on String methods for enums -->
Example 2:

```go
type CardType int

const (
CardType_UNDEFINED CardType = iota
CardType_PLASTIC
CardType_VIRTUAL
)

// CardType_UNDEFINED=0, CardType_PLASTIC=1, CardType_VIRTUAL=2
```

---
**NOTE**

You also can use the package <https://github.com/abice/go-enum>.

---
32 changes: 32 additions & 0 deletions src/enum-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# String() for enums

Prefer to implement the `String()` method for custom enum types.

```go
type CardType int

const (
CardType_UNDEFINED CardType = iota
CardType_PLASTIC
CardType_VIRTUAL
)

func (s CardType) String() string {
switch s {
case CardType_PLASTIC:
return "plastic"
case CardType_VIRTUAL:
return "virtual"
}

return "undefined"
}

```

---
**NOTE**

You also can use the package <https://github.com/abice/go-enum>.

---
8 changes: 6 additions & 2 deletions src/import-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

There should be two import groups:

- Standard library
- Everything else
- Standard(builtin) library
- External packages
- Internal packages

This is the grouping applied by goimports by default.

Expand All @@ -18,6 +19,7 @@ import (
"os"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
"your-project/internal/package"
)
```

Expand All @@ -30,6 +32,8 @@ import (

"go.uber.org/atomic"
"golang.org/x/sync/errgroup"

"your-project/internal/package"
)
```

Expand Down
29 changes: 1 addition & 28 deletions src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,6 @@ misnomer, since these conventions cover far more than just source file
formatting—gofmt handles that for us.

The goal of this guide is to manage this complexity by describing in detail the
Dos and Don'ts of writing Go code at Uber. These rules exist to keep the code
Dos and Don'ts of writing Go code. These rules exist to keep the code
base manageable while still allowing engineers to use Go language features
productively.

This guide was originally created by [Prashant Varanasi] and [Simon Newton] as
a way to bring some colleagues up to speed with using Go. Over the years it has
been amended based on feedback from others.

[Prashant Varanasi]: https://github.com/prashantv
[Simon Newton]: https://github.com/nomis52

This documents idiomatic conventions in Go code that we follow at Uber. A lot
of these are general guidelines for Go, while others extend upon external
resources:

1. [Effective Go](https://go.dev/doc/effective_go)
2. [Go Common Mistakes](https://go.dev/wiki/CommonMistakes)
3. [Go Code Review Comments](https://go.dev/wiki/CodeReviewComments)

We aim for the code samples to be accurate for the two most recent minor versions
of Go [releases](https://go.dev/doc/devel/release).

All code should be error-free when run through `golint` and `go vet`. We
recommend setting up your editor to:

- Run `goimports` on save
- Run `golint` and `go vet` to check for errors

You can find information in editor support for Go tools here:
<https://go.dev/wiki/IDEsAndTextEditorPlugins>
35 changes: 0 additions & 35 deletions src/lint.md

This file was deleted.

2 changes: 0 additions & 2 deletions src/performance.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# Performance

Performance-specific guidelines apply only to the hot path.
44 changes: 43 additions & 1 deletion src/struct-field-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ k := User{
</td></tr>
</tbody></table>

Exception: Field names *may* be omitted in test tables when there are 3 or
Exception #1: Field names *may* be omitted in test tables when there are 3 or
fewer fields.

```go
Expand All @@ -39,3 +39,45 @@ tests := []struct{
{Subtract, "subtract"},
}
```

Exception #2: Field names *should* be omitted in function-constructor

<table>
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
<tbody>
<tr><td>

```go

type Consumer struct {
logger string
svc string
}

func NewConsumer(logger string, svc string) *Consumer {
return &Consumer{
logger: logger,
svc: svc,
}
}

```

</td><td>

```go
type Consumer struct {
logger string
svc string
}

func NewConsumer(logger string, svc string) *Consumer {
return &Consumer{
logger,
svc,
}
}
```

</td></tr>
</tbody></table>
Loading