This is an example how to use Golang (go) goroutine
to run multiple services (long running) concurrently.
Start multiple services, and stop all services when receiving SIGINT
signal.
The main routine will start a goroutine for each service, and wait for all goroutines
to finish.
This mechanism allows the main routine to be able to stop all services when it receives a SIGINT
signal.
The implementation is based on channel and select
statement.
- Listen different Operating System (OS) signals
- Start main service and all child services
- Wait for all services to start
- Stop main service when receiving OS, and stop all child services
- Wait for all services to stop
- Example of single file service single_file.go
- Example of multiple files service multi_file.go
- Example of multiple files service
List all available services
go run main.go -list
Run service by name
go run main.go -run -race <service_name>
Example
go run -race main.go -run single_file
# or
go run -race main.go -run multi_file
Stop services
Once the main routine is blocking, `press Ctrl+C`` to stop all services or in a different terminal, run
kill -SIGINT <pid>
make build
binary file will be generated in build
folder
Apache License 2.0