This repository contains example code written in Go (Golang) to demonstrate various concepts and techniques. Each example is self-contained and can be used as a starting point for your own Go projects.
- Installation
- Examples
- Example 1: Goroutines
- Example 2: Channels
- Example 3: WaitGroup
- Example 4: SelectStatement
- Example 5: BufferedChannels
- Example 6: Context
- Example 7: Errors
- Example 8: WorkerPool
- Example 9: Mutex and Shared State
- Example 10: Atomic Operations
- Example 11: Rate Limiting with time.Ticker
- Example 12: Defer
- Example 13: Panic and Recover
- Example 14: Custom Types and Methods
- Example 15: JSON Marshaling and Unmarshaling
- Example 16: File I/O
- Example 17: Generics
- Contributing
- License
To run the example code, make sure you have Go installed on your system. You can download and install Go from the official website: https://golang.org/
Once Go is installed, clone this repository to your local machine using the following command:
git clone https://github.com/sprite5641/golang-examples.git
This repository contains the following examples:
This example demonstrates how to use goroutines to perform concurrent operations. It includes examples of creating goroutines, passing arguments to goroutines, and waiting for goroutines to finish.
This example demonstrates how to use channels to communicate between goroutines. It includes examples of creating channels, sending values to channels, and receiving values from channels.
This example demonstrates how to use a WaitGroup to wait for goroutines to finish. It includes examples of creating a WaitGroup, adding goroutines to a WaitGroup, and waiting for goroutines to finish.
This example demonstrates how to use a select statement to receive values from multiple channels. It includes examples of creating channels, sending values to channels, and receiving values from channels.
This example demonstrates how to use buffered channels to send and receive multiple values. It includes examples of creating buffered channels, sending values to buffered channels, and receiving values from buffered channels.
This example demonstrates how to use a context to cancel goroutines. It includes examples of creating a context, passing a context to goroutines, and canceling goroutines.
This example demonstrates how to use errors to handle unexpected conditions. It includes examples of creating errors, checking for errors, and handling errors.
This example demonstrates how to use a worker pool to perform concurrent operations. It includes examples of creating a worker pool, adding tasks to a worker pool, and waiting for tasks to finish.
This example demonstrates how to use a mutex to protect shared resources from concurrent access. It includes examples of creating a mutex, using the mutex to protect critical sections of code, and avoiding race conditions.
This example demonstrates how to use atomic operations to perform safe and efficient updates to shared variables without the need for locks.
This example demonstrates how to use a ticker to limit the rate of execution of certain operations.
This example demonstrates how to use the defer statement to ensure that a function call is executed when the surrounding function exits, regardless of the control flow.
This example demonstrates how to use the panic and recover mechanisms to handle exceptional situations gracefully.
This example demonstrates how to define custom types and methods on them.
This example demonstrates how to marshal Go structs into JSON and unmarshal JSON into Go structs.
This example demonstrates how to read and write files in Go.
This example demonstrates how to use generic in Go.
If you would like to contribute to this project, you can follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them.
- Push your changes to your fork.
- Submit a pull request.
This project is licensed under the MIT License. Feel free to use the code in this repository as a reference or starting point for your own Go projects.