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

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏