variable_parameter

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 0 Imported by: 5

README

Go可变参数

一、这是什么

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

二、安装

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

三、Example

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)
	fmt.Println(options.Foo)
}

func main() {

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

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

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TakeFirstParam

func TakeFirstParam[T any](parameters []T) T

TakeFirstParam 获取可变参数的第一个参数

func TakeFirstParamOrDefault

func TakeFirstParamOrDefault[T any](parameters []T, defaultValue T) T

TakeFirstParamOrDefault 获取可变参数的第一个参数,如果没有传的话则使用默认值

func TakeFirstParamOrDefaultFunc added in v0.0.2

func TakeFirstParamOrDefaultFunc[T any](parameters []T, defaultValueFunc func() T) T

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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