I could be wrong but this seems like over-complicating what you need. Just return the pointer to your foo struct. If you have your NewFoo return your pointer it should be accessible but not modifiable and a quick test I just did seems to validate that. Let me know if I misunderstand, in package 1 I added the base parts of what you have:
package model type foo struct { bar int } func (f *foo) GetBar() int { return f.bar } func NewFoo(bar int) *foo { return &foo{bar: bar} } In package 2 I then validated that I can only "see"/ print out the public method: func TestSimpleFoo(t *testing.T) { f := model.NewFoo(456) fmt.Printf("bar has: %v", f.GetBar()) } Potentially since you have it all in one class it is hard to "see" that it works but if you split up it will be easier? Hope that helps, let me know if I misunderstood the question. Sincerely, David On Sunday, February 24, 2019 at 3:31:10 PM UTC-6, Deomid Ryabkov wrote: > > Why can't Go resolve methods declared on the pointer receiver of the > destination type? > > consider: > > type foo struct{ > bar int > } > > type Foo *foo > > func (f *foo) GetBar() int { > return f.bar > } > > func NewFoo(bar int) Foo { > return &foo{bar: bar} > } > > func main() { > f1 := &foo{bar: 123} > fmt.Printf("%d\n", f1.GetBar()) > f2 := NewFoo(456) > fmt.Printf("%d\n", f2.GetBar()) > } > > > (playground link: https://play.golang.org/p/v0f9pYaTJAa ) > > it fails to compile with "prog.go:25:23: f2.GetBar undefined (type Foo has > no field or method GetBar)" but it's not clear to me, why Foo doesn't get > pointer methods from *foo. > -- 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. For more options, visit https://groups.google.com/d/optout.