Go using mux Router - How to pass my DB to my handlers

前端 未结 2 497
一整个雨季
一整个雨季 2021-02-07 15:50

At the moment, I try to create a small Web-Project using Go for data handling on the server.

I try to pass my database-connection to my HandlerFunc(tions) but it does no

2条回答
  •  有刺的猬
    2021-02-07 16:41

    You can always have "env" defined as global variable.

    But before everyone will hate me, this is not a good solution! You should create a package that encapsulate the access to your database with public function that state your exact intent.

    Something along the lines of

    Package db
    
    var config ....
    
    func ShowTodos(params ... ) result {
       your database access code here.... 
    }
    

    and from your router function access it with

    db.ShowTodos(...)
    

提交回复
热议问题