Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Getting Started

Robert Catmull edited this page Nov 10, 2020 · 7 revisions

Pull in the dependency

go get github.com/catmullet/go-workers

Add the import to your project

giving an alias helps since go-workers doesn't exactly follow conventions.
(If your using a JetBrains IDE it should automatically give it an alias)

import (
    worker "github.com/catmullet/go-workers"
)

Create a new worker worker

The NewWorker factory method returns a new worker.
(Method chaining can be performed on this method like calling .Work() immediately after.)

type MyWorker struct {}

func NewMyWorker() *MyWorker {
	return &MyWorker{}
}

func (my *MyWorker) Work(w *goworker.Worker) error {
	// work here
}

worker := worker.NewWorker(ctx, NewMyWorker(), numberOfWorkers)

Send work to worker

Send accepts an interface. So send it anything you want.

worker.Send("Hello World")

Close the worker when done

This closes the in channel on the worker and signals to the go functions to stop.

worker.Close()

Wait for the worker to finish and handle errors

Any error that bubbles up from your worker functions will return here.

if err := worker.Wait(); err != nil {
    //Handle error
}