Posted in News - 2 years ago
Dealing with config/options, defaults etc.. Is there any "right" way? In my latest project github.com/goware/disque, I went with "defaults" with possibility to change the settings as follows: ```go jobs, _ := disque.New("127.0.0.1:7711") config := disque.Config{ Timeout: time.Second, // Each operation fails after 1s timeout elapses. Replicate: 2, // Replicates job to 2+ nodes before Add() returns. Delay: time.Hour, // Schedules the job (enqueues after 1h). RetryAfter: time.Minute, // Re-queues the job after 1min of not being ACKed. TTL: 24 * time.Hour, // Removes the job from the queue after one day. MaxLen: 1000, // Fails if there are 1000+ jobs in the queue. } // Apply globally. jobs.Use(config) // Apply to a single operation. jobs.With(config).Add(data, "queue") // Apply single option to a single operation. jobs.Timeout(time.Second).Get("queue", "queue2") jobs.MaxLen(1000).RetryAfter(time.Minute).Add(data, "queue") jobs.TTL(24 * time.Hour).Add(data, "queue") ``` Fabrizio Milo (@Mistobaan) reached me on gophers.slack.com and suggested "functional options" this to me..

Functional options for friendly APIs

What follows is the text of my presentation, Functional options for friendly APIs that I gave at dotGo this year. It has been edited slightly for readability. I want to thank Kelsey Hightower, Bill Kennedy, Jeremy Saenz, and Brian Ketelsen, for their assistance in preparing this talk.