decorator

package
v0.0.0-...-91e790a Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2022 License: MIT Imports: 1 Imported by: 0

README

通用装饰器

  • 反射实现的一个通用装饰器,性能较定制化的装饰器较差
package main

import (
	"fmt"
	"time"

	"github.com/ACV-er/gotools/decorator"
)

func print(s string) int {
	fmt.Println(s)

	sum := 0
	for i := 0; i < 1000; i++ {
		sum += i
	}

	return sum
}

func count_cost(target func()) {
	start_time := time.Now().UnixNano()

	target()

	end_time := time.Now().UnixNano()

	fmt.Printf("耗时: %d纳秒\n", (end_time - start_time))
}

func _count_cost(target func(string) int) func(string) int {
	return func(s string) int {
		start_time := time.Now().UnixNano()

		ret := target(s)

		end_time := time.Now().UnixNano()

		fmt.Printf("耗时: %d纳秒\n", (end_time - start_time))

		return ret
	}
}

func main() {
	fmt.Println("---------反射装饰器---------")
	print_with_count := decorator.AddDecorator(count_cost, print)
	ret := print_with_count("这是一个字符串\n")
	fmt.Printf("%#v\n", ret)

	fmt.Println("---------闭包装饰器---------")
	func_decorator := _count_cost(print)
	iret := func_decorator("这是一个字符串\n")
	fmt.Printf("%#v\n", iret)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDecorator

func AddDecorator(decorator func(func()), target interface{}) func(...interface{}) []interface{}

go 通用装饰器

Types

This section is empty.

Jump to

Keyboard shortcuts

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