I still find the lack of any ability to reference a field in a struct is
sort of crippling - everything else feels like a work-around.

Because you have the source-code elements already, inventing "something
else" (whatever that is) so you can attach information (metadata) to those
source-code elements, is always going to feel like a work-around - strings
as field-names are awkward and unreliable, because you have a way to
reference fields already, it just doesn't "work".

No matter what else I come up with as work-arounds for various problems,
this is lacking.

I also noticed the Alignof() and Offsetof() functions in the unsafe<http://godoc.org/unsafe>package, and look: they get to do it!

Offsetof returns the offset within the struct of the field represented by
v, which must be of the form structValue.field. In other words, it returns
the number of bytes between the start of the struct and the start of the
There is only one way these functions are getting both the struct and the
field passed to them - these functions are not real "functions" in the Go
sense, and what you're passing to these function-calls is actually two
arguments, the struct and the field, although for these special functions
you get to write it as one actual argument.

That's what I'm missing.

So you can argue it's not necessary and that there are other ways to solve
my problems, but there isn't really - there is currently only one way to
designate a field (not it's value) in a struct, and that is by using
strings. In a statically-typed language, using strings to reference types
should not be necessary.

I think I will try to summarize this stuff in a feature request, because
clearly Go itself needs this functionality and had to create some internal
work-around to get it - this "ArbitraryType" type, which isn't a real type,
and doesn't work in userland.

I learned lots of other useful things from this conversation, but I also
learned that there is no (reliable) way to reference fields for the
purposes of reflection.

I think that needs to be fixed.

On Tuesday, December 31, 2013 7:54:54 PM UTC-5, Rasmus Schultz wrote:

Is there any way to *statically *reference a type or member?

Something like a typeof() operator or something?

I realize something like reflect.TypeOf(Foo{}).FieldByName("xyz") is
possible, but the field-name can't be checked at compile-time...

Any other alternatives?
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts


Follow ups

Related Discussions



site design / logo © 2022 Grokbase