2

I have following code:

A struct controller, that have anonymous fields.

type controller struct {
    *base.Controller
    store *data
}

As you can see, *base.Controller is anonymous fields pointer. Then a second struct that composed with *base.Controller too.

type expiredError struct {
    *base.Controller
    local string
}

I initialized the controller struct as follow:

c := &controller{base.New(rw, r, false, "controller/account"), nil}
c.Title = "Activate account"
c.serve()

The base.Controller on controller is initialize with base.New() function.

Now I have on controller methods, that initialize the expiredError struct and it should share the pointer of base.Controller to base.Controller of expiredError too.

func (rcv *controller) validate() error {

    ....

    // If time for activating account is expired
    if time.Now().Unix() > rcv.store.Expired {
        // Delete registered user from neo4j
        maccount.Delete(rcv.store.Email, rcv.Local)

        return &expiredError{base.Controller, rcv.Local}
    }

    return nil
}

I've got here a compiler error

type base.Controller is not an expression
softshipper
  • 32,463
  • 51
  • 192
  • 400
  • `type base.Controller is not an expression`, means you need an instance of something there, not the type. – JimB Jan 02 '15 at 21:49

1 Answers1

3

You can try referencing the anonymous field by mentioning the instance of the parent class:

rcv.Controller

(since the "name" of an anonymous field is the same as the type of the field)

See a similar example in:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    You can only access embedded fields by their type name, and type names can't have selectors. Only `rcv.Controller` could work. – JimB Jan 02 '15 at 21:46
  • 1
    @JimB indeed. I have edited the answer accordingly. I should have known since I used a similar example in http://stackoverflow.com/a/27733969/6309 yesterday. – VonC Jan 02 '15 at 21:48