No need for unsafe:
https://play.golang.org/p/7cTejtO-I1z
You can't *modify* the field, but you can read it just fine. In fact,
that's how fmt does it as well:
https://play.golang.org/p/cuFQMsvLmaZ
I suspect you are seeing the error you are seeing because you are either
trying to modify it (calling one of the Set* methods of reflect.Value) or
use reflect.Value.Interface, which I guess isn't allowed because it might
enable you to call methods on that unexported field value and modifying it
or something?
https://play.golang.org/p/b2fzyZHZWJs
Either way, if you switch on the reflect.Value.Kind() and use the
.String/.Int/.Bool etc., you can read the value at least.

On Sat, Feb 8, 2020 at 9:07 PM Tamás Gulácsi <tgulacs...@gmail.com> wrote:

>
>
> 2020. február 8., szombat 20:42:32 UTC+1 időpontban Alexander Mills a
> következőt írta:
>>
>> it looks like this way works:
>>
>> https://stackoverflow.com/a/43918797/12211419
>>
>> know of any others?
>>
>
> Yup, that's one - get the address of the field and get that.
>
> Another is to declare a similar (memory and type-wise) structure with
> exported fields,
> and unsafe-type-cast it: https://play.golang.org/p/z0QG-CP_MSL
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/836756d6-0654-4341-8a2e-af57ff07a231%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/836756d6-0654-4341-8a2e-af57ff07a231%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEw%2B7nCX_spugEHRs1QO3LrPvyQkj4irHUjKsJfSo38Aw%40mail.gmail.com.

Reply via email to