Good afternoon, I use sessions from here.

"github.com/gorilla/sessions" WbSocket "code.google.com/p/go.net/websocket" 

At first I created a session, as it is written on the office. This option works:

  session, _ := store.Get(r, "SessionId") // r- Request log.Println("Значение из сессии Id=", session.Values["Id"], " и FIO=", session.Values["FIO"]) 

It is necessary to work with this data and I save them ... 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("Клиент подключился сокет=", Soc_Id, ".id- ", Id, " ФИО- ", FIO, " Роль- ", 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 too, is the same. Why does it output the string type to the console, and then it swears?

Update Thank you Vadim Shender, decided

 Id , _ := session.Values["Id"].(string) FIO , _ := session.Values["FIO"].(string) Role , _ := session.Values["Role"].(string) 

    1 answer 1

    Because if you look at the source code of the library, session.Values is of type map[interface{}]interface{} so that it can store any values, that is, the values ​​you get Id , FIO , Role are of type interface{} , as more general, not string .

    The fact that these values ​​are normally output using Println is a consequence of the fact that all these values ​​implement the Stringer interface.

    If you know what type of data is actually stored in a variable of type interface{} , you can explicitly convert this type using type assertion . For example, knowing that FIO is a string, you can explicitly convert to this type, and then use it as a string.

     FIOstr, ok := FIO.(string) if ok { // пробразование успешно, в FIO на самом деле была строка // FIOstr имеет тип string // и вы можете его использовать как строковое значение FIO } 

    You can do the same with the rest of the fields, for example, if you have all of them string, then the code will be something like this (I omit checking for errors here, but note if the type is wrong, without checking, your program will crash):

     ... Id := session.Values["Id"].(string) FIO := session.Values["FIO"].(string) Role := session.Values["Role"].(string) sockCli := clientConn{ws, Soc_Id, Id, FIO, Role} ... 

    PS I strongly advise you to read at least this . I even found it in Russian when I got it in Google.

    • Thank you very much. I wrote Id, _: = session.Values ​​["Id"]. (String) FIO, _: = session.Values ​​["FIO"]. (String) Role, _: = session.Values ​​["Role"] . (string) - Rakzin Roman
    • Note that the program will not crash in case of an error, but the string variables will be empty. - Vadim Shender