Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

499
Views
panic: error al analizar uri: el esquema debe ser "mongodb" o "mongodb+srv" Golang: error de conexión de MongoDB

Este es el error que recibo cuando docker-compongo mi aplicación en docker-desktop.

 { { }} panic: error parsing uri: scheme must be "mongodb" or "mongodb+srv" goroutine 1 [running]: go-products/database.ConnectDB(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) /app/database/database.go:15 +0x145 main.main() /app/main.go:14 +0x13d

Aquí está mi archivo main.go

 package main import ( "fmt" "github.com/gorilla/mux" "go-products/config" "go-products/database" "net/http" ) func main() { conf := config.GetConfig() fmt.Println(conf) db := database.ConnectDB(conf.Mongo) fmt.Println(db) r := mux.NewRouter() http.ListenAndServe(":8080", r) }

Este es el archivo de la base de datos donde trato de hacer la conexión.

 package database import ( "context" "go-products/config" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func ConnectDB(conf config.MongoConfiguration) *mongo.Database { connection := options.Client().ApplyURI(conf.Server) client, err := mongo.Connect(context.TODO(), connection) if err != nil { panic(err) } return client.Database(conf.Database) }

archivo config.go

 package config import "github.com/spf13/viper" type Configuration struct { Environment string Mongo MongoConfiguration } type MongoConfiguration struct { Server string Database string Collection string } func GetConfig() Configuration { conf := Configuration{} viper.SetConfigName("config") viper.SetConfigType("yml") viper.AddConfigPath("./config") err := viper.ReadInConfig() if err != nil { panic(err) } return conf }

este es el archivo config.yaml .

 environment: dev mongo: server: mongodb://mongo:27017 database: Mgo collection: Products

Revisé la documentación oficial y algunos recursos, pero no pude encontrar una manera de manejar esto en el entorno de Golang . ¿Qué debo cambiar aquí?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En su función GetConfig() , declara una variable de tipo Configuration y la devuelve, pero en realidad nunca asigna nada a ninguno de sus campos. Es por eso que su llamada fmt.Println() para volcar la configuración solo muestra { { }} ; ninguno de los campos tiene valores asignados.

Debe descomponer la configuración de Viper en la estructura de configuración:

 func GetConfig() (Configuration, error) { var conf Configuration // ... viper setup ... err := viper.ReadInConfig() if err != nil { return Configuration{}, err } // *** unmarshal the Viper config into the Configuration struct *** err = viper.Unmarshal(&conf) if err != nil { return Configuration{}, err } // *** return conf, nil }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!