栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Golang / MGO —恐慌:没有可访问的服务器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Golang / MGO —恐慌:没有可访问的服务器

运行您发布的代码的以下版本。尽量不要修改代码,至少不要更改行号的位置。这样,如果您发布堆栈跟踪,则数字将匹配。

package mainimport (    "fmt"    "time")import (    "labix.org/v2/mgo")func connectToMongo() bool {    ret := false    fmt.Println("enter main - connecting to mongo")    // tried doing this - doesn't work as intended    defer func() {        if r := recover(); r != nil { fmt.Println("Detected panic") var ok bool err, ok := r.(error) if !ok {     fmt.Printf("pkg:  %v,  error: %s", r, err) }        }    }()    maxWait := time.Duration(5 * time.Second)    session, sessionErr := mgo.DialWithTimeout("localhost:27017", maxWait)    if sessionErr == nil {        session.SetMode(mgo.Monotonic, true)        coll := session.DB("MyDB").C("MyCollection")        if ( coll != nil ) { fmt.Println("Got a collection object") ret = true        }    } else { // never gets here        fmt.Println("Unable to connect to local mongo instance!")    }    return ret}func main() {    if ( connectToMongo() ) {        fmt.Println("Connected")    } else {        fmt.Println("Not Connected")    }}

当MongoDB启动时,我看到:

enter main - connecting to mongoGot a collection objectConnected

当MongoDB关闭时,我看到:

enter main - connecting to mongoUnable to connect to local mongo instance!Not Connected

如果您没有看到相同的行为,请发布输出,包括看到的恐慌。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484448.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号