Skip to content

mateusfigmelo/go_by_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go By Examples

GoLang

Go Version License PRs Welcome Stars

Go Reference Go Report Card Last Commit Examples

A comprehensive collection of Go programming examples

Learn Go by example: from basic concepts to advanced patterns and best practices

Examples

  1. Hello World - Basic program structure
  2. Data Types - Types, variables, and constants
  3. Control Structures - Flow control and branching
  4. Collections - Arrays, slices, and maps
  5. Functions - Function declarations and usage
  6. Types and Pointers - Memory management
  7. Structs and Interfaces - Custom types and abstraction
  8. Generics and Embedding - Generic programming and composition
  9. Error Handling - Error types and handling
  10. Concurrency - Goroutines and basic concurrency
  11. Advanced Channels - Channel patterns and usage
  12. Worker Pools - Concurrent worker patterns
  13. Sync Primitives - Mutexes and synchronization
  14. Sorting - Sorting algorithms and interfaces
  15. Panic, Defer, Recover - Error flow control
  16. Strings and Templates - String manipulation
  17. JSON and XML - Data serialization
  18. Time Handling - Time operations
  19. Parsing and Random - Text parsing and randomization
  20. Hashes and Encoding - Cryptography basics
  21. File Operations - File system handling
  22. File Utilities - Advanced file operations
  23. Testing and Benchmarking - Advanced testing
  24. CLI - Command-line interfaces
  25. Environment and Logging - Configuration and logs
  26. HTTP Client and Server - HTTP client/server
  27. Context and Processes - Process management
  28. Signals and Exit - Signal handling

Prerequisites

  • Go 1.21 or higher (some examples use newer features)
  • Basic understanding of Go programming concepts
  • Text editor or IDE
  • Terminal/Command Prompt

Running Examples

Each example directory contains:

  • Source code files (.go)
  • README with explanations and instructions
  • Tests where applicable
  • Any necessary resource files

To run an example:

cd examples/<example-directory>
go run main.go

Testing

Each example includes tests that demonstrate proper usage and validate functionality. We use Go's built-in testing framework with some extended features.

Running Tests

You can run tests using our Go test runner:

# Run all tests
go run main.go

# Run tests for a specific example
go run main.go 12-worker-pools

# Run tests with coverage
go run main.go -c

# Get verbose test output
go run main.go -v

# See all options
go run main.go -h

Test Coverage

Coverage reports are generated in HTML format when running tests with the -c or --coverage flag. These reports are saved in the coverage/ directory.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-example)
  3. Make your changes and add appropriate tests
  4. Ensure all tests pass (go run main.go)
  5. Commit your changes (git commit -m 'Add a new example')
  6. Push to the branch (git push origin feature/amazing-example)
  7. Create a Pull Request

Best Practices

Each example follows these principles:

  • Clear and documented code
  • Proper error handling
  • Comprehensive unit tests
  • Table-driven testing where appropriate
  • Consistent formatting (using go fmt)
  • Idiomatic Go patterns
  • Performance considerations
  • Security best practices

Testing Best Practices

  • Write tests for all exported functions
  • Use table-driven tests for multiple scenarios
  • Aim for high code coverage (at least 80%)
  • Test both happy paths and error cases
  • Keep tests independent and idempotent
  • Use mocks or stubs for external dependencies
  • Include benchmarks for performance-critical code
  • Write clear failure messages

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Go By Example

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages