goki.dev/laser@v0.1.34/README.md (about) 1 # laser: reflection optimized 2 3 [](https://goreportcard.com/report/goki.dev/laser) 4 <a href="https://pkg.go.dev/goki.dev/laser"><img src="https://img.shields.io/badge/dev-reference-007d9c?logo=go&logoColor=white&style=flat" alt="pkg.go.dev docs"></a> 5 6 Package laser is a highly reflective package of golang reflect helpers (lasers work by bouncing light back and forth between two mirrors). It also connotes the "lazy" aspect of reflection -- running dynamically instead of statically precompiled. It is derived from the [ki/kit](https://github.com/goki/ki/tree/v1) package in v1 of Ki. 7 8 As usual, Go [reflect](https://pkg.go.dev/reflect) provides just the minimal API for dealing with reflection, and there are several well-documented issues that require a bit of non-obvious logic to get around. 9 10 Some example functions: 11 12 * `AnyIsNil()` -- checks if interface value is `nil` -- requires extra logic for multiple levels of nil. 13 14 * `ValueIsZero()` -- checks for any kind of zero, including `nil`. 15 16 * `ToInt, ToFloat, ToString, ToBool` -- robustly converts any value to given type, using an efficient type switch for all basic types, including pointers, and using `strconv` parse from string. See also [glop/num](https://github.com/goki/glop/tree/num) for generics-based conversion. The key difference is that if you have an `any`, you can't use generics, so these type-switch methods are necessary in that case. 17 18 * `SetRobust(to, frm any) bool` -- robustly sets the 'to' value from the 'from' value, for any case, using the To* functions and JSON format for maps and slices. 19 20 * `UnhideAnyValue(v reflect.Value) reflect.Value` -- ensures value is actually assignable -- e.g., `reflect.Make*` functions return a pointer to the new object, but it is hidden behind an interface{} and this magic code extracts the actual underlying value 21 22 23