Created
May 31, 2022 06:56
-
-
Save ulexxander/9fabe2d455ee33bb4e4c587f5a756129 to your computer and use it in GitHub Desktop.
Good way to organize Pub/Sub subjects for both publishers and consumers. Can be applied to other OOP languages as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api | |
import "fmt" | |
func publish(subject string) { | |
fmt.Println("Publishing on", subject) | |
} | |
func subscribe(subject string) { | |
fmt.Println("Subscribing on", subject) | |
} | |
func example() { | |
var subjects Subjects | |
publish(subjects.NoteByID("123")) | |
publish(subjects.NoteCreate()) | |
subscribe(subjects.NoteUpdate("*")) | |
subscribe(subjects.Time("eu", "ljubljana")) | |
} | |
// Subjects builds subjects. | |
// Wildcards can be passed as parameters. | |
type Subjects struct{} | |
func (Subjects) NoteList() string { | |
return "note.list" | |
} | |
func (Subjects) NoteByID(id string) string { | |
return "note.by_id." + id | |
} | |
func (Subjects) NoteCreate() string { | |
return "note.create" | |
} | |
func (Subjects) NoteUpdate(id string) string { | |
return "note.update." + id | |
} | |
func (Subjects) NoteDelete(id string) string { | |
return "note.delete." + id | |
} | |
// Example from https://docs.nats.io/nats-concepts/subjects | |
func (Subjects) Time(region, city string) string { | |
return "time." + region + "." + city | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment