Answer the question
In order to leave comments, you need to log in
How to correctly call a function?
Hello.
I want to receive value from a DB from separate function.
Here is the db query function:
func getusers(login string) string {
client := redis.NewClient(&redis.Options{
Addr: conf.DBREDIS_PORT,
Password: conf.PASSWORD_REDIS,
DB: 0,
})
id, _ := client.HGet("admin:"+login, "id").Result()
return id
}
func RendMA(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "sess")
login := session.Values["AName"]
idp := getusers(login)
fmt.Println("id", idp)
}
Answer the question
In order to leave comments, you need to log in
The types are different, but the compiler tells you about this.
login := session.Values["AName"] where login is interface{}
while func getusers(login string) takes a string argument.
so you need to write a function like this
func RendMA(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "sess")
login := session.Values["AName"]
if loginStr, ok := login.(string); ok {
idp := getusers(loginStr)
fmt.Println("id", idp)
} else {
//
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question