Download pkg/pointer/pointer.go from xiaozhian/kpl: direct link, hf CLI and curl.
- Browser
- Download file 423 Bytes
-
https://huggingface.co/spaces/xiaozhian/kpl/resolve/main/pkg/pointer/pointer.go
- Command line
-
hf download hf://spaces/xiaozhian/kpl/pkg/pointer/pointer.go
-
curl -L -o pointer.go https://huggingface.co/spaces/xiaozhian/kpl/resolve/main/pkg/pointer/pointer.go
423 Bytes
| package pointer | |
| import "reflect" | |
| func To[T any](v T) *T { | |
| vo := reflect.ValueOf(v) | |
| for { | |
| if vo.Kind() == reflect.Ptr { | |
| vo = vo.Elem() | |
| } else { | |
| break | |
| } | |
| } | |
| if vo.IsZero() { | |
| return nil | |
| } | |
| return &v | |
| } | |
| func Take[T any](v *T) T { | |
| if v == nil { | |
| t := reflect.TypeOf(v) | |
| _v := reflect.New(t) | |
| for t.Kind() == reflect.Ptr { | |
| t = t.Elem() | |
| _v = _v.Elem() | |
| } | |
| return _v.Interface().(T) | |
| } | |
| return *v | |
| } | |