In the following code I declare a var and then use it IF I have access to the database. Otherwise the entire process is pointless. I can’t declare the admin var at the top which is where I like all vars defined, nor can I declare it inside the else condition to get this error to quit pestering me.
I see plenty of people climbing a high horse declaring that go rocks because it forces you to write good code but to me this makes zero sense and I have no idea how to make it quit doing stupid stuff like this. Or maybe I’m being stupid and not seeing something.
I can add
_ = admin
to make the error go away but seriously, is this an example of clean code?
Please help me understand how to deal with situations like this as they seem to be happening to me regularly.
thanks,
var admin bool
if err = db.Ping(); err != nil {
logging.LogError( 0, 500, "No Database Connection Exists", w )
return
} else {
vars := req.Header.Get("userhash")
if isAdminUser( vars, w ) {
rows, err = querytopmenuall.Query( vars )
admin = true
} else {
rows, err = querytopmenu.Query( vars )
admin = false
}