doujiao1981 2016-04-04 22:39
浏览 35
已采纳

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.

  • 写回答

1条回答 默认 最新

  • douhong1703 2016-04-04 22:42
    关注

    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) {
      ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统