Uber Go Guide #50

Open
opened 2021-06-25 17:35:13 +00:00 by jeff · 0 comments
Owner

https://github.com/uber-go/guide/blob/master/style.md

Use defer to clean up resources.

Use the time package for handling time. Use duration for durations.

When adding context to returned errors, keep the context succinct by avoiding phrases like "failed to", which state the obvious and pile up as the error percolates up through the stack.

The single return value form of a type assertion will panic on an incorrect type. Therefore, always use the "comma ok" idiom.

Don't panic. Panic/recover is not an error handling strategy.

Avoid Embedding Types in Public Structs.

If using init, make sure it is deterministic. (The precomputed table for ternary bianry words.)

Call one of os.Exit or log.Fatal* only in main(). All other functions should return errors to signal failure.

strconv is faster than fmt.

Group Similar Declarations.

Short and succinct package names.

Reduce Nesting (keep code left).

Put a line between embedded types and others.

Use short := declaration unless declaring the zero value.

Reduce scope of variables, especially err.

Avoid naked parameters.

Use Raw String Literals to Avoid Escaping.

Use Field Names to Initialize Structs.

Use make to initialize maps. This avoids panics when writing to a nil map.

https://github.com/uber-go/guide/blob/master/style.md Use defer to clean up resources. Use the time package for handling time. Use duration for durations. When adding context to returned errors, keep the context succinct by avoiding phrases like "failed to", which state the obvious and pile up as the error percolates up through the stack. The single return value form of a type assertion will panic on an incorrect type. Therefore, always use the "comma ok" idiom. Don't panic. Panic/recover is not an error handling strategy. Avoid Embedding Types in Public Structs. If using init, make sure it is deterministic. (The precomputed table for ternary bianry words.) Call one of os.Exit or log.Fatal* only in main(). All other functions should return errors to signal failure. strconv is faster than fmt. Group Similar Declarations. Short and succinct package names. Reduce Nesting (keep code left). Put a line between embedded types and others. Use short `:=` declaration unless declaring the zero value. Reduce scope of variables, especially `err`. Avoid naked parameters. Use Raw String Literals to Avoid Escaping. Use Field Names to Initialize Structs. Use make to initialize maps. This avoids panics when writing to a nil map.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: simplesystems/go-resources#50
No description provided.