Preamble:
Go does not support const/immutable pointers.
Not a problem, because...
When I need to return a pointer reference to a chunk of data while
enforcing read-only access:
- I place my data in an unexported struct type with exported getters.
- I return a simple struct wrapper with an embedded pointer field.
..and there is virtually no speed penalty thanks to recent inlining
improvements in the compiler!!!
e.g.
type item {
a int
}
func (this *item) A() int {
return this.a
}
type readOnlyItem {
*item
}
func myfunc() GetItem() readOnlyItem {
return readOnlyItem {&myitem}
}
This works great in practice.
However, if I replace readOnlyItem above with an anonymous struct.
I get the following oddities:
- My package compiles ok.
- Importers of my package fail to compile with the following errors at the
import line.
unknown struct {} field 'mypackage.item' in struct literal
tempname called with nil type
Anonymous structs are so much cleaner in these situations and I cannot see
why they shouldn't work.
I have seen bug reports on the above errors, but they are not quite
identical to the above scenario.
If this is specified behaviour, should my package not fail to compile?
Or is this indeed a bug?
Any insights would be appreciated.
TIA
--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.