Search code examples
gomux

Undefined return type in Go


I'm fairly new to Go and am having trouble with a code snippet that uses mux-gorilla sessions/cookies. There is a lot of redundancy that I would like to reduce with the following function:

func isLoggedIn(w http.ResponseWriter, r *http.Request) (bool, *Session) {
    session, err := store.Get(r, "user")
    var logged bool = true
    if err != nil { // Need to delete the cookie.
        expired := &http.Cookie{Path: "/", Name: "user", MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
        http.SetCookie(w, expired)
        logged := false
    }
    return logged, session
}

Unfortunately I get the following compilation error: undefined: Session

How can this type be undefined if it can be returned by the store.Get function? Note that store was declared before as store = sessions.NewCookieStore([]byte(secret)), using the "gorilla/sessions" package.


Solution

  • Go needs to know which package to find Session in: sessions.Session.

    The error is on the signature of your func isLoggedIn

    So your modified code would be:

    import "github.com/gorilla/sessions"
    func isLoggedIn(w http.ResponseWriter, r *http.Request) (bool, *sessions.Session) {
      ...
    }