I have this structure:
├── app
│ ├── app.go
│ ├── handler
│ │ ├── common.go
│ │ └── users.go
│ ├── model
│ │ └── model.go
│ └── queries
│ └── queries.go
├── config
│ └── config.go
└── main.go
All my queries (SQL ones) are in queries.go
:
queries.go:
package queries
const (
QueryFindUser = `SELECT * FROM users WHERE id=$1` // <------ HERE
)
users.go:
package handler
import (
"GO_APP/app/model"
"GO_APP/app/queries"
"fmt"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jmoiron/sqlx"
)
var db *sqlx.DB
// getUserOr404 gets a User instance if exists, or respond the 404 error otherwise
func getUserOr404(db *sqlx.DB, id int, w http.ResponseWriter, r *http.Request) (*model.User, error) {
user := model.User{}
err := db.Get(&user, queries.QueryFindUser, id) // <----- Here
return &user, err
}
If I use QueryFindUser
(capital first letter) then it will become exportable to all the other packages. But I don't want this to be accessible by say config/config.go
I want it to be restricted to handler
's modules only exlcluding the model/model.go
.
If I declare queryFindUser
this will be limited to the package queries
only and then I won't be able to use it anywhere.
How to solve this thing? Without making it exportable to every other package. I am quite new to golang/java and this issue will be same in java as well. So what to do in this case? If you can provide resources that will also be helpful to me.