dssk35460 2019-02-13 04:32
浏览 301
已采纳

两种不同形式的数据库连接字符串

I have two different codes to connect postgresql with golang first code like this

connStr := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbName)

and the second one like this

connStr := fmt.Sprintf("postgres://%s:%s@localhost/%s?sslmode=disable", user, password, dbName)

What's the difference between them? And when do I have to use the second one?

  • 写回答

1条回答 默认 最新

  • douyun8901 2019-02-13 07:43
    关注

    Both of them work and as far as I know there is not real difference between them.

    import "database/sql"
    
    import _ "github.com/lib/pq"
    
    connStr := fmt.Sprintf("postgres://%s:%s@localhost/%s?sslmode=disable", user, password, dbName)
    db, err := sql.Open("postgres", connStr)
    

    ///////////////////////////////

    import "database/sql"
    
    import _ "github.com/lib/pq"
    
    connStr := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbName)
    db, err := sql.Open("postgres", connStr)
    if err != nil {
        log.Fatal(err)
    }
    

    More info you can find here: https://godoc.org/github.com/lib/pq

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器