go-publisher

command module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2017 License: MIT Imports: 10 Imported by: 0

README

go-publisher GoDoc

Type-safe publisher/subscriber generator for golang.

Installation

go get -d github.com/kamichidu/go-publisher
make -C $GOPATH/src/github.com/kamichidu/go-publisher install

Usage

go-publisher -o OutputFilename.go -t YourPublisherTypeName EventName1 EventName2

Documentation

Overview

go-publisher generates type-safe publisher/subscriber implementation in Go.

Usage:

go-publisher [flags] [event ...]

The flags are:

-o
	Output filename.
-p
	Package name.
-t
	Publisher type name.
-v
	Print version info.
-tags
	Go build tags.

Debugging support:

-no-gofmt
	Generate Go source code without formatting.

Examples

package main

import (
	"context"
	"log"
)

type subscriber struct{}

func (self *subscriber) OnEventA(ctx context.Context) error {
	log.Printf("On%s with no args", ExampleEventA)
	return nil
}

func (self *subscriber) OnEventB(ctx context.Context, arg1 string, arg2 int) error {
	log.Printf("On%s with %v", ExampleEventB, []interface{}{arg1, arg2})
	return nil
}

//go:generate go-publisher -o example_publisher.go -t Example EventA 'EventB|arg1:string,arg2:int'
func ExampleUsage() {
	subscription := NewExampleSubscription()
	subscription.Subscribe(&subscriber{})
	log.Printf("publish %s", ExampleEventA)
	if err := subscription.PublishEventA(context.Background()); err != nil {
		log.Printf("Publish%s: %s", ExampleEventA, err)
	}
	log.Printf("publish %s", ExampleEventB)
	if err := subscription.PublishEventB(context.Background(), "example", 9999); err != nil {
		log.Printf("PublishEventB: %s", err)
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL