• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D18-Jul-201822

.travis.ymlH A D18-Jul-2018216

Gopkg.lockH A D18-Jul-2018438

Gopkg.tomlH A D18-Jul-2018747

LICENSEH A D18-Jul-201811.1 KiB

README.mdH A D18-Jul-20182.5 KiB

go_above_17.goH A D18-Jul-2018171

go_above_19.goH A D18-Jul-2018257

go_below_17.goH A D18-Jul-2018136

go_below_19.goH A D18-Jul-2018244

reflect2.goH A D18-Jul-20187.1 KiB

reflect2_amd64.sH A D18-Jul-20180

reflect2_kind.goH A D18-Jul-2018916

relfect2_386.sH A D18-Jul-20180

relfect2_amd64p32.sH A D18-Jul-20180

relfect2_arm.sH A D18-Jul-20180

relfect2_arm64.sH A D18-Jul-20180

relfect2_mips64x.sH A D18-Jul-20180

relfect2_mipsx.sH A D18-Jul-20180

relfect2_ppc64x.sH A D18-Jul-20180

relfect2_s390x.sH A D18-Jul-20180

safe_field.goH A D18-Jul-20181.2 KiB

safe_map.goH A D18-Jul-20182.5 KiB

safe_slice.goH A D18-Jul-20182.5 KiB

safe_struct.goH A D18-Jul-2018781

safe_type.goH A D18-Jul-20181.7 KiB

test.shH A D18-Jul-2018321

type_map.goH A D18-Jul-20183 KiB

unsafe_array.goH A D18-Jul-20182 KiB

unsafe_eface.goH A D18-Jul-20181.3 KiB

unsafe_field.goH A D18-Jul-20182.1 KiB

unsafe_iface.goH A D18-Jul-20181.3 KiB

unsafe_link.goH A D18-Jul-20182.7 KiB

unsafe_map.goH A D18-Jul-20184.1 KiB

unsafe_ptr.goH A D18-Jul-20181 KiB

unsafe_slice.goH A D18-Jul-20185.2 KiB

unsafe_struct.goH A D18-Jul-20181.5 KiB

unsafe_type.goH A D18-Jul-20182.2 KiB

README.md

1# reflect2
2
3[![Sourcegraph](https://sourcegraph.com/github.com/modern-go/reflect2/-/badge.svg)](https://sourcegraph.com/github.com/modern-go/reflect2?badge)
4[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/modern-go/reflect2)
5[![Build Status](https://travis-ci.org/modern-go/reflect2.svg?branch=master)](https://travis-ci.org/modern-go/reflect2)
6[![codecov](https://codecov.io/gh/modern-go/reflect2/branch/master/graph/badge.svg)](https://codecov.io/gh/modern-go/reflect2)
7[![rcard](https://goreportcard.com/badge/github.com/modern-go/reflect2)](https://goreportcard.com/report/github.com/modern-go/reflect2)
8[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://raw.githubusercontent.com/modern-go/reflect2/master/LICENSE)
9
10reflect api that avoids runtime reflect.Value cost
11
12* reflect get/set interface{}, with type checking
13* reflect get/set unsafe.Pointer, without type checking
14* `reflect2.TypeByName` works like `Class.forName` found in java
15
16[json-iterator](https://github.com/json-iterator/go) use this package to save runtime dispatching cost.
17This package is designed for low level libraries to optimize reflection performance.
18General application should still use reflect standard library.
19
20# reflect2.TypeByName
21
22```go
23// given package is github.com/your/awesome-package
24type MyStruct struct {
25	// ...
26}
27
28// will return the type
29reflect2.TypeByName("awesome-package.MyStruct")
30// however, if the type has not been used
31// it will be eliminated by compiler, so we can not get it in runtime
32```
33
34# reflect2 get/set interface{}
35
36```go
37valType := reflect2.TypeOf(1)
38i := 1
39j := 10
40valType.Set(&i, &j)
41// i will be 10
42```
43
44to get set `type`, always use its pointer `*type`
45
46# reflect2 get/set unsafe.Pointer
47
48```go
49valType := reflect2.TypeOf(1)
50i := 1
51j := 10
52valType.UnsafeSet(unsafe.Pointer(&i), unsafe.Pointer(&j))
53// i will be 10
54```
55
56to get set `type`, always use its pointer `*type`
57
58# benchmark
59
60Benchmark is not necessary for this package. It does nothing actually.
61As it is just a thin wrapper to make go runtime public.
62Both `reflect2` and `reflect` call same function
63provided by `runtime` package exposed by go language.
64
65# unsafe safety
66
67Instead of casting `[]byte` to `sliceHeader` in your application using unsafe.
68We can use reflect2 instead. This way, if `sliceHeader` changes in the future,
69only reflect2 need to be upgraded.
70
71reflect2 tries its best to keep the implementation same as reflect (by testing).