I have a struct:
type user struct {
Id string
..
data_ptr *userData
}
And I store slice of users in global scope:
type Hall struct {
users []user
}
var hall = Hall{} //global
Finally, http handler:
func dataHandler(response http.ResponseWriter, request *http.Request) {
userExist, user_ptr := hall.haveUserId() //works fine
switch requestType {
case "load":
user_ptr.loadData() //data loaded and user_ptr.data_ptr is set
case "newData":
user_ptr.data_ptr = newData // <-- this is it, now previously set data_ptr == nil
So, why the heck, I mean I send "load" request, it loads data, sets data_ptr
for user_ptr
. But on next call, "newData" request, user_ptr.data_ptr
is nil
?
Just in case, here is loadData()
:
func (p *user) loadData(userId) {
..
data := userData {}
p.data_ptr = &data
}
EDIT: where user_ptr
comes from:
func (h *Hall) haveUserId(id string) (bool, *user) {
for _, u := range h.users {
if u.Id == id {
fmt.Println("UID found")
return true, &u
}
}
return false, nil
}