Giter Club home page Giter Club logo

go-variable-parameter's Introduction

Go可变参数

一、这是什么

这个库为Go中的可变参数提供了一些辅助方法,以便更爽的使用可变参数。

二、安装

go get -u github.com/golang-infrastructure/go-variable-parameter

三、Example

3.1 TakeFirstParamOrDefault

package main

import (
	"fmt"
	variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
)

type FooOptions struct {
	Foo string
	Bar int
}

var DefaultFooOptions = &FooOptions{
	Foo: "default foo",
	Bar: 10,
}

func Foo(optionsVariableParams ...*FooOptions) {
	// 如果传递了options则使用传递的取出数组中的第一个元素返回,如果没传递则使用给出的默认值,适合默认值是一个固定的值的时候用
	options := variable_parameter.TakeFirstParamOrDefault(optionsVariableParams, DefaultFooOptions)

	// 后面的代码就可以直接使用options来操作啦
	fmt.Println(options.Foo)

}

func main() {

	// 不传递参数
	Foo() // Output: default foo

	// 传递参数
	Foo(&FooOptions{Foo: "custom foo"}) // Output: custom foo

}

3.2 TakeFirstParamOrDefaultFunc

package main

import (
	"fmt"
	variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
	"time"
)

type FooOptions struct {
	Foo string
	Bar int
}

func NewFooOptions() *FooOptions {

	// 假装有耗时操作
	time.Sleep(time.Second * 1)

	return &FooOptions{
		Foo: "default foo",
		Bar: 10,
	}
}

func Foo(optionsVariableParams ...*FooOptions) {
	// 如果后面可能会涉及到对options的修改之类的,则options无法使用单例,可能得每次都重新创建一个新的,则可以使用一个默认值的函数,仅在需要默认值的时候才运行
	options := variable_parameter.TakeFirstParamOrDefaultFunc(optionsVariableParams, func() *FooOptions {
		// 如果初始化比较耗时或者觉得仅在必要的时候才创建比较好可以用这种默认函数的方式,此函数仅在未传递参数时运行
		return NewFooOptions()
	})

	// 后面的代码就可以直接使用options来操作啦
	fmt.Println(options.Foo)
}

func main() {

	// 传递参数
	start := time.Now()
	Foo(&FooOptions{Foo: "custom foo"})
	cost := time.Now().Sub(start)
	fmt.Println("传递参数时耗时:" + cost.String())
	// Output:
	// custom foo
	// 传递参数时耗时:0s

	// 不传递参数
	start = time.Now()
	Foo()
	cost = time.Now().Sub(start)
	fmt.Println("不传递参数时耗时:" + cost.String())
	// Output:
	// default foo
	// 不传递参数时耗时:1.0136631s

}

3.3 SetDefaultParam

package main

import (
	"fmt"
	variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
)

type FooOptions struct {
	Foo string
	Bar int
}

var DefaultFooOptions = &FooOptions{
	Foo: "default foo",
	Bar: 10,
}

func Foo(options ...*FooOptions) {
	// 如果没有传递参数的话,则设置一个默认的参数
	options = variable_parameter.SetDefaultParam(options, DefaultFooOptions)

	// 为什么不使用这种方式来操作呢?也许这样更简单?
	// 在递归互相调用的时候切片中可能会被重复放入默认值,尤其是在有很多的重载之类的或者高内聚的代码中会互相调用问题尤其明显
	//options = append(options, DefaultFooOptions)

	// 后面的代码就可以直接使用options[0]来操作而不必担心越界
	fmt.Println(options[0].Foo)
}

func main() {

	// 传递参数
	Foo(&FooOptions{Foo: "custom foo"}) // Output: custom foo

	// 不传递参数
	Foo() // Output: default foo

}

3.4 SetDefaultParamByFunc

package main

import (
	"fmt"
	variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
	"time"
)

type FooOptions struct {
	Foo string
	Bar int
}

func NewFooOptions() *FooOptions {

	// 假装有耗时操作
	time.Sleep(time.Second * 1)

	return &FooOptions{
		Foo: "default foo",
		Bar: 10,
	}
}

func Foo(options ...*FooOptions) {
	// 如果后面可能会涉及到对options[0]的修改之类的,则options[0]无法使用单例,可能得每次都重新创建一个新的,则可以使用一个默认值函数,仅在未传递参数的时候才会执行
	options = variable_parameter.SetDefaultParamByFunc(options, func() *FooOptions {
		// 如果初始化比较耗时或者觉得仅在必要的时候才创建比较好可以用这种默认函数的方式,此函数仅在未传递参数时运行
		return NewFooOptions()
	})

	// 后面的代码就可以直接使用options[0]来操作啦
	fmt.Println(options[0].Foo)
}

func main() {

	// 传递参数
	start := time.Now()
	Foo(&FooOptions{Foo: "custom foo"})
	cost := time.Now().Sub(start)
	fmt.Println("传递参数时耗时:" + cost.String())
	// Output:
	// custom foo
	// 传递参数时耗时:0s

	// 不传递参数
	start = time.Now()
	Foo()
	cost = time.Now().Sub(start)
	fmt.Println("不传递参数时耗时:" + cost.String())
	// Output:
	// default foo
	// 不传递参数时耗时:1.0136631s

}

go-variable-parameter's People

Contributors

cc11001100 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.