Sebastiaan van Stijn 35b238ee82
vendor: vendor with -compat=1.17
This might break compatibility with projects using this module that
are still on go1.16, which is EOL, so probably ok to ignore:

    github.com/docker/buildx/store imports
        github.com/gofrs/flock tested by
        github.com/gofrs/flock.test imports
        gopkg.in/check.v1 loaded from gopkg.in/check.v1@v1.0.0-20200227125254-8fa46927fb4f,
        but go 1.16 would select v1.0.0-20201130134442-10cb98267c6c

    To upgrade to the versions selected by go 1.16:
        go mod tidy -go=1.16 && go mod tidy -go=1.17
    If reproducibility with go 1.16 is not needed:
        go mod tidy -compat=1.17
    For other options, see:
        https://golang.org/doc/modules/pruning

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
..
.gitignore vendor: initial vendor
.travis.yml vendor: update buildkit to 2f99651
Gopkg.lock vendor: update buildkit to 2f99651
Gopkg.toml vendor: update buildkit to 2f99651
LICENSE vendor: initial vendor
README.md vendor: initial vendor
go_above_19.go vendor: update buildkit to 2f99651
go_above_118.go vendor: update buildkit to 2f99651
go_below_118.go vendor: update buildkit to 2f99651
reflect2.go vendor: update buildkit to 2f99651
reflect2_amd64.s vendor: initial vendor
reflect2_kind.go vendor: initial vendor
relfect2_386.s vendor: initial vendor
relfect2_amd64p32.s vendor: initial vendor
relfect2_arm.s vendor: initial vendor
relfect2_arm64.s vendor: initial vendor
relfect2_mips64x.s vendor: initial vendor
relfect2_mipsx.s vendor: initial vendor
relfect2_ppc64x.s vendor: initial vendor
relfect2_s390x.s vendor: initial vendor
safe_field.go vendor: initial vendor
safe_map.go vendor: initial vendor
safe_slice.go vendor: initial vendor
safe_struct.go vendor: initial vendor
safe_type.go vendor: initial vendor
type_map.go vendor: update buildkit to 2f99651
unsafe_array.go vendor: initial vendor
unsafe_eface.go vendor: initial vendor
unsafe_field.go vendor: initial vendor
unsafe_iface.go vendor: initial vendor
unsafe_link.go vendor: update buildkit to 2f99651
unsafe_map.go vendor: update buildkit to 2f99651
unsafe_ptr.go vendor: initial vendor
unsafe_slice.go vendor: initial vendor
unsafe_struct.go vendor: initial vendor
unsafe_type.go vendor: initial vendor

README.md

reflect2

Sourcegraph GoDoc Build Status codecov rcard License

reflect api that avoids runtime reflect.Value cost

  • reflect get/set interface{}, with type checking
  • reflect get/set unsafe.Pointer, without type checking
  • reflect2.TypeByName works like Class.forName found in java

json-iterator use this package to save runtime dispatching cost. This package is designed for low level libraries to optimize reflection performance. General application should still use reflect standard library.

reflect2.TypeByName

// given package is github.com/your/awesome-package
type MyStruct struct {
	// ...
}

// will return the type
reflect2.TypeByName("awesome-package.MyStruct")
// however, if the type has not been used
// it will be eliminated by compiler, so we can not get it in runtime

reflect2 get/set interface

valType := reflect2.TypeOf(1)
i := 1
j := 10
valType.Set(&i, &j)
// i will be 10

to get set type, always use its pointer *type

reflect2 get/set unsafe.Pointer

valType := reflect2.TypeOf(1)
i := 1
j := 10
valType.UnsafeSet(unsafe.Pointer(&i), unsafe.Pointer(&j))
// i will be 10

to get set type, always use its pointer *type

benchmark

Benchmark is not necessary for this package. It does nothing actually. As it is just a thin wrapper to make go runtime public. Both reflect2 and reflect call same function provided by runtime package exposed by go language.

unsafe safety

Instead of casting []byte to sliceHeader in your application using unsafe. We can use reflect2 instead. This way, if sliceHeader changes in the future, only reflect2 need to be upgraded.

reflect2 tries its best to keep the implementation same as reflect (by testing).