reflect

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package reflect extends the standard reflect package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoEqual added in v0.21.0

func DoEqual(x, y interface{}) (err error)

DoEqual is a naive interfaces comparison that check and use Equaler interface and return an error if its not match.

func IsEqual

func IsEqual(x, y interface{}) bool

IsEqual is a naive interfaces comparison that check and use Equaler interface.

func IsNil

func IsNil(v interface{}) bool

IsNil will return true if v's type is chan, func, interface, map, pointer, or slice and its value is `nil`; otherwise it will return false.

Example
package main

import (
	"errors"
	"fmt"
	"net/http"
)

type F func()

type T struct{}

func (t *T) J() bool {
	return true
}

func main() {
	var (
		aBoolean   bool
		aChannel   chan int
		aFunction  F
		aMap       map[int]int
		aPtr       *T
		aSlice     []int
		anInt      int
		emptyError error
		fs         http.FileSystem
	)

	cases := []struct {
		v interface{}
	}{
		{}, // Unitialized.
		{v: aBoolean},
		{v: aChannel},          // Uninitialized channel.
		{v: aFunction},         // Empty func type.
		{v: aMap},              // Unitialized map.
		{v: make(map[int]int)}, // Initialized map.
		{v: aPtr},              // Uninitialized pointer to struct.
		{v: &T{}},              // Initialized pointer to struct.
		{v: aSlice},            // Unitialized slice.
		{v: make([]int, 0)},    // Initialized slice.
		{v: anInt},
		{v: emptyError},
		{v: errors.New("e")}, // Initialized error
		{v: fs},              // Unitialized interface type to interface{}
	}

	for _, c := range cases {
		fmt.Printf("%19T: v == nil is %5t, IsNil() is %5t\n", c.v, c.v == nil, IsNil(c.v))
	}

}
Output:

<nil>: v == nil is  true, IsNil() is  true
               bool: v == nil is false, IsNil() is false
           chan int: v == nil is false, IsNil() is  true
          reflect.F: v == nil is false, IsNil() is  true
        map[int]int: v == nil is false, IsNil() is  true
        map[int]int: v == nil is false, IsNil() is false
         *reflect.T: v == nil is false, IsNil() is  true
         *reflect.T: v == nil is false, IsNil() is false
              []int: v == nil is false, IsNil() is  true
              []int: v == nil is false, IsNil() is false
                int: v == nil is false, IsNil() is false
              <nil>: v == nil is  true, IsNil() is  true
*errors.errorString: v == nil is false, IsNil() is false
              <nil>: v == nil is  true, IsNil() is  true

Types

type Equaler

type Equaler interface {
	IsEqual(v interface{}) bool
}

Equaler is an interface that when implemented by a type, it will be used to compare the value in Assert.

Jump to

Keyboard shortcuts

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