function

package
v6.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package function supplies some convenient functions, such as calling a function or method dynamically, and getting a integer range between the maximal and the minimum.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFunc is returned when the callee is not a function.
	ErrNotFunc = errors.New("The first argument is not the function")

	// ErrArgsNum is returned when the number of the arguments is incorrect
	// when calling the callee.
	ErrArgsNum = errors.New("The number of the argument is incorrect")

	// ErrArgsType is returned when the type of the arguments is incorrect
	// when calling the callee.
	ErrArgsType = errors.New("The type of the argument is incorrect")
)
View Source
var (
	// ErrNotPointer is returned when the value is not a pointer.
	ErrNotPointer = fmt.Errorf("the value is not a pointer")

	// ErrNotSliceOrArray is returned when the value is not a slice or array.
	ErrNotSliceOrArray = fmt.Errorf("The value is not slice or array")

	// ErrInvalidIndex is returned when the index exceeds over the length of
	// the slice.
	ErrInvalidIndex = fmt.Errorf("the index is exceeds the length of the slice")

	// ErrTypeNotCompatible is returned when the type is not compatible.
	ErrTypeNotCompatible = fmt.Errorf("the type is not compatible")
)
View Source
var (
	Split        = strings2.Split
	SplitN       = strings2.SplitN
	SplitSpace   = strings2.SplitSpace
	SplitSpaceN  = strings2.SplitSpaceN
	SplitString  = strings2.SplitSpace
	SplitStringN = strings2.SplitSpaceN
)

Some string splitting functions.

For backward compatibility, we redefine them to those in strings2. See the sub-package strings2.

View Source
var (
	// ErrNotHaveMethod is returned when a certain type doesn't have the method.
	ErrNotHaveMethod = errors.New("Don't have the method")
)

Functions

func BindMapToStruct

func BindMapToStruct(value interface{}, m map[string]interface{}) (err error)

BindMapToStruct binds a map to struct.

Notice: it uses SetValue to update the field and supports the json tag.

func Call

func Call(f interface{}, args ...interface{}) (results []interface{}, err error)

Call calls a function dynamically.

Example
f := func(i int, j int) (int, error) {
	return i + j, errors.New("This is not an error")
}

ret, _ := Call(f, 1, 2)

// Since the first result is an integer, and it's not necessary to check
// whether it is nil, so you may omit it, and infer this type directly.
if ret[0] != nil {
	fmt.Println(ret[0].(int))
}

// Since the second result may be nil, so you MUST check whether it is nil firstly.
if ret[1] != nil {
	fmt.Println(ret[1].(error))
}
Output:

3
This is not an error

func CallMethod

func CallMethod(t interface{}, method string, args ...interface{}) ([]interface{}, error)

CallMethod calls the method 'method' of 't', and return (ReturnedValue, nil) if calling successfully, which ReturnedValue is the result which that method returned. Or return (nil, Error).

func Compare

func Compare(first, second interface{}) int

Compare compares two values.

The returned value is

an positive integer when first is greater than second,
0  when they are equal.
an negative integer when first is less than second.

It supports these types as follow:

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64
float32 float64
Comparer

Notice: the two values must have the same type and not be nil, or panic.

func EQ

func EQ(first, second interface{}) bool

EQ reports whether the two values are equal.

It's the convenient function of Compare, and will panic if there is an error.

Example
fmt.Println(EQ(1, 1))
fmt.Println(EQ(1, 2))
Output:

true
false

func GE

func GE(first, second interface{}) bool

GE reports whether first is greater than or equal to second.

It's the convenient function of Compare, and will panic if there is an error.

Example
fmt.Println(GE(1, 1))
fmt.Println(GE(1, 2))
fmt.Println(GE(2, 1))
Output:

true
false
true

func GT

func GT(first, second interface{}) bool

GT reports whether first is greater than second.

It's the convenient function of Compare, and will panic if there is an error.

Example
fmt.Println(GT(1, 1))
fmt.Println(GT(1, 2))
fmt.Println(GT(2, 1))
Output:

false
false
true

func GetMethod

func GetMethod(t interface{}, method string) interface{}

GetMethod returns the method, `method`, of `t`. If not, return nil.

Notice: The first argument of the returned function is the receiver. That's, when calling the function, you must pass the receiver as the first argument of that, but, which the passed receiver needs not be identical to t.

func GetSliceValue

func GetSliceValue(slice interface{}, i int) (interface{}, error)

GetSliceValue returns the ith element of slice.

If slice is not a slice or array type, it will return ErrNotSliceOrArray. If the index i exceeds over the length of slice, that's, i>=len(slice), it will return ErrInvalidIndex.

For []interface{}, []string and []int, they have already been optimized.

func HasMethod

func HasMethod(t interface{}, method string) bool

HasMethod returns true if `t` has the method of `method`.

func InSlice

func InSlice(v interface{}, slice interface{}) bool

InSlice returns true if v is in slice, or returns false.

It returns false if slice is not a slice type or the type is not compatible.

For []interface{}, []string and []int, they have already been optimized.

Example
ss := []interface{}{"hello", "world"}
fmt.Println(InSlice("hello", ss))
fmt.Println(InSlice("aaron", ss))
Output:

true
false

func IsNil

func IsNil(v interface{}) bool

IsNil returns true when v is the nil value.

Example
var i *int
var v interface{} = i

fmt.Printf("i == %v\n", i)
fmt.Printf("v == %v\n", v)

fmt.Printf("i == nil  ==>  %v\n", i == nil)
fmt.Printf("v == nil  ==>  %v\n", v == nil)

fmt.Printf("IsNil(i)  ==>  %v\n", IsNil(i))
fmt.Printf("IsNil(v)  ==>  %v\n", IsNil(v))
Output:

i == <nil>
v == <nil>
i == nil  ==>  true
v == nil  ==>  false
IsNil(i)  ==>  true
IsNil(v)  ==>  true

func LE

func LE(first, second interface{}) bool

LE reports whether first is less than or equal to second.

It's the convenient function of Compare, and will panic if there is an error.

Example
fmt.Println(LE(1, 1))
fmt.Println(LE(1, 2))
fmt.Println(LE(2, 1))
Output:

true
true
false

func LT

func LT(first, second interface{}) bool

LT reports whether first is less than second.

It's the convenient function of Compare, and will panic if there is an error.

Example
fmt.Println(LT(1, 1))
fmt.Println(LT(1, 2))
fmt.Println(LT(2, 1))
Output:

false
true
false

func PullSliceValue

func PullSliceValue(out interface{}, slice interface{}, i int) error

PullSliceValue gets the ith element from the slice and puts it into the variable out, then returns nil if successfully.

Return an error If out isn't a pointer, or slice isn't a slice or array type, or the length of slice is less than or equal to the index i, or the type of slice[i] and the underlying type of out are not compatible.

Example
ss := []int{1, 2, 3, 4, 5, 6}
out := -1
err := PullSliceValue(&out, ss, 1)
fmt.Println(out, err)

out = -1
err = PullSliceValue(&out, ss, 6)
fmt.Println(out, err)
Output:

2 <nil>
-1 the index is exceeds the length of the slice

func PullSliceValueWithDefault

func PullSliceValueWithDefault(out interface{}, slice interface{}, i int,
	_default interface{}) error

PullSliceValueWithDefault is the same as PullSliceValue, but if index >= len(slice), set the value of out to _default.

Example
ss := []int{1, 2, 3, 4, 5, 6}
out := 0
err := PullSliceValueWithDefault(&out, ss, 1, -1)
fmt.Println(out, err)

out = 0
err = PullSliceValueWithDefault(&out, ss, 6, -1)
fmt.Println(out, err)
Output:

2 <nil>
-1 <nil>

func Range

func Range(num int, others ...int) []int

Range collects three kinds of the using of Range.

Range(stop)              ==> Ranges(0, num, 1)
Range(start, stop)       ==> Ranges(start, stop, 1)
Range(start, stop, step) ==> Ranges(start, stop, step)

Notice: it is equal to range in Python.

Example
fmt.Println(Ranges(1, 10, 2))
fmt.Println(Ranges(10, 1, -2))
Output:

[1 3 5 7 9]
[10 8 6 4 2]

func Ranges

func Ranges(start, stop, step int) (r []int)

Ranges returns a integer range between start and stop, which progressively increase or descrease by step.

If step is positive, r[i] = start + step*i when i>0 and r[i]<stop.

If step is negative, r[i] = start + step*i but when i>0 and r[i]>stop.

If step is 0, it will panic.

Example
fmt.Println(Range(10))
fmt.Println(Range(1, 10))
fmt.Println(Range(1, 10, 1))
fmt.Println(Range(1, 10, 2))
fmt.Println(Range(10, 0))
Output:

[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[1 3 5 7 9]
[]

func Reverse

func Reverse(slice interface{}) interface{}

Reverse reverses the source slice then returns it.

The argument must be a slice or array. Or it will panic.

Example
// []interface{}
s1 := []interface{}{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s1))
s1 = []interface{}{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s1))

// []string
s2 := []string{"a", "b", "c", "d", "e", "f"}
fmt.Println(Reverse(s2))
s2 = []string{"a", "b", "c", "d", "e", "f", "g"}
fmt.Println(Reverse(s2))

// []int
s3 := []int{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s3))
s3 = []int{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s3))

// []int64
s4 := []int64{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s4))
s4 = []int64{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s4))

// []uint
s5 := []uint{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s5))
s5 = []uint{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s5))

// []uint64
s6 := []uint64{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s6))
s6 = []uint64{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s6))

// [n]byte
s7 := [6]byte{1, 2, 3, 4, 5, 6}
fmt.Println(Reverse(s7))
s8 := [7]byte{1, 2, 3, 4, 5, 6, 7}
fmt.Println(Reverse(s8))
Output:

[6 5 4 3 2 1]
[7 6 5 4 3 2 1]
[f e d c b a]
[g f e d c b a]
[6 5 4 3 2 1]
[7 6 5 4 3 2 1]
[6 5 4 3 2 1]
[7 6 5 4 3 2 1]
[6 5 4 3 2 1]
[7 6 5 4 3 2 1]
[6 5 4 3 2 1]
[7 6 5 4 3 2 1]
[6 5 4 3 2 1]
[7 6 5 4 3 2 1]

func SetStructValue

func SetStructValue(s interface{}, attr string, v interface{}) error

SetStructValue is equal to SetStructValueOf(reflect.ValueOf(s), attr, v)

func SetStructValueOf

func SetStructValueOf(s reflect.Value, attr string, v interface{}) error

SetStructValueOf is the same as SetValue, but binds the attribute attr of the struct s to v.

func SetValue

func SetValue(v interface{}, data interface{}, ignoreNil ...bool) (err error)

SetValue binds data to v which must be a pointer.

The converting rule between the types of data and v:

bool, string, number           ->  *bool
bool, string, number, []byte   ->  *string
bool, string, number, []byte   ->  *[]byte
bool, string, number           ->  *float32
bool, string, number           ->  *float64
bool, string, number           ->  *int
bool, string, number           ->  *int8
bool, string, number           ->  *int16
bool, string, number           ->  *int32
bool, string, number           ->  *int64
bool, string, number           ->  *uint
bool, string, number           ->  *uint8
bool, string, number           ->  *uint16
bool, string, number           ->  *uint32
bool, string, number           ->  *uint64
string, time.Time              ->  *time.Time
map[string]string              ->  *map[string]string
map[string]string              ->  *map[string]interface{}
map[string]interface{}         ->  *map[string]interface{}

Notice: number stands for all the integer and float types.

For bool, "t", "T", "1", "on", "On", "ON", "true", "True", "TRUE", "yes", "Yes", and "YES" are true, and "f", "F", "0", "off", "Off", "OFF", "false", "False", "FALSE", "no", "No", "NO", and "" are false. Others is invalid.

For time.Time, it supports the layout ISO8601 and RFC3339. If it's ISO8601, the time must be UTC. So you can parse the time as follow:

var t1, t2 time.Time
SetValue(&t1, "2019-01-16T15:39:40Z")
SetValue(&t2, "2019-01-16T15:39:40+08:00")

If v support the interface SetValuer, it will call its SetValue method.

Notice: if data is nil and ignoreNil is true, it will do nothing and return nil.

func Valid

func Valid(f interface{}, args ...interface{}) (vf reflect.Value, vargs []reflect.Value, err error)

Valid valids whether the callee is a function, and the number the type of the arguments is correct, then return the valid function, the valid arguments and nil.

Types

type Comparer

type Comparer interface {
	// Compare two values.
	//
	// The returned value is
	//   an positive integer when it is greater than value,
	//   0  when they are equal.
	//   an negative integer when it is less than value.
	Compare(value interface{}) int
}

Comparer is used to compare two values.

type SetValuer

type SetValuer interface {
	SetValue(v interface{}) error
}

SetValuer is used to set the itself value to v.

Jump to

Keyboard shortcuts

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