K
K
Kagtaviy2016-04-30 11:28:28
go
Kagtaviy, 2016-04-30 11:28:28

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
}

To enter this function, you need to specify login which is taken from the session.
func RendMA(w http.ResponseWriter, r *http.Request) {
  session, _ := store.Get(r, "sess")

  login := session.Values["AName"]
  idp := getusers(login)

  fmt.Println("id", idp)
}

The compiler complains about this part idp := getusers(login)
cannot use login (type interface {}) as type string in argument to getusers: need type assertion (build)at line 25 col 1
Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Semchenko, 2016-04-30
@Kagtaviy

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 question

Ask a Question

731 491 924 answers to any question