Skip to content

Instantly share code, notes, and snippets.

@bdougie
Created March 6, 2025 18:31
Show Gist options
  • Select an option

  • Save bdougie/70ab34d7debf2e50efff52f8b53937b7 to your computer and use it in GitHub Desktop.

Select an option

Save bdougie/70ab34d7debf2e50efff52f8b53937b7 to your computer and use it in GitHub Desktop.
• Use gofmt to format your code automatically. Don’t fight the formatter.
• Write clear and concise package names. Use lowercase, single-word names without underscores or mixed caps.
• Follow Go naming conventions:
• Use MixedCaps or mixedCaps for multiword names, not underscores
• Capitalize exported names
• Use short, descriptive names for variables and functions
• Utilize Go’s built-in error handling. Return errors as values, not exceptions.
• Take advantage of multiple return values, especially for error handling.
• Use defer for cleanup operations like closing files or unlocking mutexes.
• Prefer composition over inheritance. Use embedding to include functionality from other types.
• Make use of interfaces for abstraction. Define interfaces for the methods you need.
• Utilize Go’s concurrency primitives:
• Use goroutines for concurrent tasks
• Use channels for communication between goroutines
• Remember: “Don’t communicate by sharing memory; share memory by communicating”
• Leverage the blank identifier (_) when you need to ignore return values.
• Use type switches to handle multiple types in interfaces.
• Write clear documentation comments for packages, functions, and types.
• Avoid unnecessary else clauses. Handle errors early and return.
• Use named return values when it improves clarity.
• Make good use of the init() function for package initialization when needed.
• Remember that semicolons are mostly implicit in Go. Don’t add them unnecessarily.
• Use the built-in testing package for unit tests.
• Utilize Go’s powerful slice and map types effectively.
• Remember to check for nil before dereferencing pointers.
• Use the context package for managing cancellations and deadlines across API boundaries.
• Follow the principle of “Accept interfaces, return structs” in your API design.
• Use go vet and golint tools regularly to catch common mistakes and style issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment