A go library that facilitates the implementation of decorator pattern.

go-decorator

go-decorator is a library that facilitates the implementation of decorator pattern.

Installation

To install go-decorator, use go get:

go get -u github.com/garenchan/go-decorator

Usage

package main

import (
	"fmt"

	"github.com/garenchan/go-decorator"
)

func sum(a, b int) int {
	fmt.Printf("%d + %d\n", a, b)
	return a + b
}

func main() {
	mySum := sum

        // Use TimeIT to decorate sum function, and get a new function mySum
	decorator.Decorate(&mySum, sum, decorator.TimeIt)

	// mySum will print the cost time.
	ret := mySum(1, 2)

	// Here will print 3.
	fmt.Println(ret)
}

Decorator

You can implement your own decorators for reuse, for example:

= 2 { divisor := in[1].Interface().(int) if divisor == 0 { return []reflect.Value{reflect.ValueOf(0)} } } return targetFunc.Call(in) } func main() { myDiv := div decorator.Decorate(&myDiv, div, safeDiv) // Here will print 0. fmt.Println(myDiv(1, 0)) // Here will print 1. fmt.Println(myDiv(1, 1)) } ">
package main

import (
	"fmt"
	"reflect"

	"github.com/garenchan/go-decorator"
)

// div integer division
func div(a, b int) int {
	return a / b
}

// safeDiv returns 0 when the divisor is 0
func safeDiv(targetFunc reflect.Value, in []reflect.Value) (out []reflect.Value) {
	if len(in) >= 2 {
		divisor := in[1].Interface().(int)
		if divisor == 0 {
			return []reflect.Value{reflect.ValueOf(0)}
		}
	}

	return targetFunc.Call(in)
}

func main() {
	myDiv := div
	decorator.Decorate(&myDiv, div, safeDiv)

	// Here will print 0.
	fmt.Println(myDiv(1, 0))

	// Here will print 1.
	fmt.Println(myDiv(1, 1))
}
Owner
Garen Chan
White fat, full of hope :snowman:
Garen Chan
Similar Resources

A tool that facilitates building OCI images

A tool that facilitates building OCI images

Buildah - a tool that facilitates building Open Container Initiative (OCI) container images The Buildah package provides a command line tool that can

Jan 3, 2023

KinK is a helper CLI that facilitates to manage KinD clusters as Kubernetes pods. Designed to ease clusters up for fast testing with batteries included in mind.

KinK is a helper CLI that facilitates to manage KinD clusters as Kubernetes pods. Designed to ease clusters up for fast testing with batteries included in mind.

kink A helper CLI that facilitates to manage KinD clusters as Kubernetes pods. Table of Contents kink (KinD in Kubernetes) Introduction How it works ?

Dec 10, 2022

forward - facilitates proxying DNS messages to upstream resolvers.

forward Name forward - facilitates proxying DNS messages to upstream resolvers. Description The forward plugin re-uses already opened sockets to the u

Oct 16, 2021

A simple api built in Go that facilitates directly sending email from your client side html to your inbox

go-email-service A simple api built in Go that facilitates directly sending emai

Dec 28, 2021

Go implementation of Fowler's Money pattern

Go implementation of Fowler's Money pattern

Money GoMoney provides ability to work with monetary value using a currency's smallest unit. This package provides basic and precise Money operations

Jan 3, 2023

🚦 Semaphore pattern implementation with timeout of lock/unlock operations.

🚦 Semaphore pattern implementation with timeout of lock/unlock operations.

🚦 semaphore Semaphore pattern implementation with timeout of lock/unlock operations. 💡 Idea The semaphore provides API to control access to a shared

Dec 7, 2022

The implementation of the pattern observer

Event This is package implements pattern-observer Fast example import ( "github.com/agoalofalife/event" ) func main() { // create struct e := even

Dec 4, 2022

An efficient and feature complete Hystrix like Go implementation of the circuit breaker pattern.

An efficient and feature complete Hystrix like Go implementation of the circuit breaker pattern.

Circuit Circuit is an efficient and feature complete Hystrix like Go implementation of the circuit breaker pattern. Learn more about the problems Hyst

Dec 28, 2022

A library that implements the outboxer pattern in go

Outboxer Outboxer is a go library that implements the outbox pattern. Getting Started Outboxer was designed to simplify the tough work of orchestratin

Dec 16, 2022

A Go library to iterate over potentially nested map keys using the visitor pattern

A Go library to iterate over potentially nested map keys using the visitor pattern

Mar 15, 2022

A convenient library to do a must pattern

must A convenient library to do a must pattern Problems Before Go 1.18, if you want to panic when the regular expression cannot compile, you need to d

Nov 15, 2022

Resequencer - A Go library that implements the resequencer pattern

resequencer A Go resequencer Introduction resequencer is a Go library that imple

Mar 19, 2022

This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.

observer This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event. Usage go get githu

Nov 16, 2022

Go language implementation of a blockchain based on the BDLS BFT protocol. The implementation was adapted from Ethereum and Sperax implementation

BDLS protocol based PoS Blockchain Most functionalities of this client is similar to the Ethereum golang implementation. If you do not find your quest

Oct 14, 2022

Multi-String Pattern Matching Algorithm Using TrieHashNode

Multi-String Pattern Matching algorithm. This implementation is inspired from Aho-Corasick algorithm Getting Started modelA = mspm.NewModel("mspm_mode

Dec 9, 2022

:incoming_envelope: A fast Message/Event Hub using publish/subscribe pattern with support for topics like* rabbitMQ exchanges for Go applications

Hub 📨 A fast enough Event Hub for go applications using publish/subscribe with support patterns on topics like rabbitMQ exchanges. Table of Contents

Dec 17, 2022

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

Introduction skipmap is a high-performance concurrent map based on skip list. In typical pattern(one million operations, 90%LOAD 9%STORE 1%DELETE), th

Jan 8, 2023

A pluggable backend API that enforces the Event Sourcing Pattern for persisting & broadcasting application state changes

A pluggable backend API that enforces the Event Sourcing Pattern for persisting & broadcasting application state changes

A pluggable "Application State Gateway" that enforces the Event Sourcing Pattern for securely persisting & broadcasting application state ch

Nov 1, 2022

A pluggable backend API that enforces the Event Sourcing Pattern for persisting & broadcasting application state changes

A pluggable backend API that enforces the Event Sourcing Pattern for persisting & broadcasting application state changes

A pluggable "Application State Gateway" that enforces the Event Sourcing Pattern for securely persisting & broadcasting application state changes

Nov 1, 2022
Asn.1 BER and DER encoding library for golang.

WARNING This repo has been archived! NO further developement will be made in the foreseen future. asn1 -- import "github.com/PromonLogicalis/asn1" Pac

Nov 14, 2022
Cap'n Proto library and parser for go. This is go-capnproto-1.0, and does not have rpc. See https://github.com/zombiezen/go-capnproto2 for 2.0 which has rpc and capabilities.

Version 1.0 vs 2.0 Update 2015 Sept 20: Big news! Version 2.0 of the go-bindings, authored by Ross Light, is now released and newly available! It feat

Nov 29, 2022
Go library for decoding generic map values into native Go structures and vice versa.

mapstructure mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling. This l

Jan 1, 2023
GED - Global-purpose Encoding / Decoding library

GED - Global-purpose Encoding / Decoding library This library lets you use common encoding/decoding schemes and allows you to define custom ones. Use

Nov 28, 2021
A library that provides dynamic features of Go language.

go-dynamic go-dynamic is a library that provides dynamic features of Go language. Installation To install go-dynamic, use go get: go get -u github.com

Dec 8, 2021
Go-ant-pattern: An ant pattern parser

go-ant-pattern - An ant pattern parser. Usage package main import ( "fmt" "github.com/cbuschka/go-ant-pattern" ) func main() { path := "

Dec 7, 2021
Leader-follower-pattern - Build leader-follower system pattern with etcd election

主备系统模式 原理 使用分布式锁实现主备节点系统。通过对分布式锁进行续期,保持长期锁, 从而使当前服务节点处于主服务节点 无法获取分布式锁的服务节点,则作为备选

Jan 24, 2022
Go-library that facilitates the usage of .env files

Goenv Golang-library that facilitates the use of .env files. Installation go get github.com/fabioelizandro/goenv Usage Place a .env file in the root

Nov 7, 2021
HTTP middleware for Go that facilitates some quick security wins.

Secure Secure is an HTTP middleware for Go that facilitates some quick security wins. It's a standard net/http Handler, and can be used with many fram

Jan 3, 2023
Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types.

Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types. Read th

Dec 27, 2022