Golang + Mongo | panic: panic: no reachable servers

45 Views Asked by At

My ideas were this: 1)Mongodb not started(it was started) 2)I wrote wrong port(i double-checked - port was correct) 3)I though that syntax is wrong, I went here(it says, that syntax is correct).

I tried to change code, as here. It says:

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

And now i dont have any ideas:(

$go run main.go

panic: no reachable servers

goroutine 1 [running]:
main.getSession(...)
/home/beton/work/github.com/user/mongo-golang/main.go:30
main.main()
/home/beton/work/github.com/user/mongo-golang/main.go:15 +0x1c5
exit status 2

file main.go

package main

import (
     "net/http"

     "github.com/julienschmidt/httprouter"
     "gopkg.in/mgo.v2"

     "github.com/betonomochalka/mongo-golang/controllers")

func main() {
     r := httprouter.New()

     uc := controllers.NewUserController(getSession())

     r.GET("/user/:id", uc.GetUser)

     r.POST("/user", uc.CreateUser)

     r.DELETE("/user/:id", uc.DeleteUser)

     http.ListenAndServe("localhost:8080", r)}

     func getSession() *mgo.Session {
         s, err := mgo.Dial(":27017")
     }

     if err != nil {
         panic(err)
     }

     return s
}

$sudo systemctl status mongod

Active: active (running) since Mon 2024-03-18 00:02:58 CET; 57min ago

$mongosh

Current Mongosh Log ID:65f7848e2d998360b9db83af
Connecting to:      mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.0
Using MongoDB:      7.0.6
Using Mongosh:      2.2.0

Need help to fix it!

0

There are 0 best solutions below