Answer the question
In order to leave comments, you need to log in
Golang Error - cannot use Id (type interface {}) as type string in field value: need type assertion?
Good afternoon, I use sessions from here - "github.com/gorilla/sessions"
websocket "code.google.com/p/go.net/websocket"
First I created a session, as it is written on the site.
This option works
- session, _ := store.Get(r, "SessionId") // r- Request
log.Println("Value from session Id=",session.Values["Id"]," and FIO=",session.Values[" FIO"])
I still need to work with this data and I save it...
But this option does not work
var ActiveClients = make(map[int ] ClientConn)
type ClientConn struct {
websocket *websocket.Conn
Socket_Id int
User_Id string
FIO string
Role string
}
func SockServer(ws *websocket.Conn) {
session, _ := store.Get(r, "SessionId")
Id =session.Values["Id"]
FIO =string(session.Values["FIO"])
Role =session.Values["Role"]
sockCli := ClientConn{ws, Soc_Id,Id,FIO,Role}
ActiveClients[Soc_Id] = sockCli
log.Println("Client connected socket=",Soc_Id,". id- ",Id ," Full Name- ",FIO," Role- ",Role)
}
Writes
cannot use Id (type interface {}) as type string in field value: need type assertion
here sockCli := ClientConn{ws, Soc_Id,Id,FIO, Role}
And for FIO and Role is the same.
Why does it output the string type to the console, but then it swears?
Answer the question
In order to leave comments, you need to log in
Found the answer en.stackoverflow.com/questions/436325
Id , _ := session.Values["Id"].(string)
FIO , _ := session.Values["FIO"].(string)
Role , _ := session.Values["Role"].(string)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question