I am very new to Go language. I am struggling with following usecase.
I want to store session id (string) and reqeust No (string) in a map for duplicate checking. When there is no entry in the map, it should create one, else it should return a flag saying the entry already exists.
package main
import "fmt"
func main() {
mySessionData := make(map[string]map[string]bool)
var ret bool
ret = chkDuplicate(mySessionData, "session1", "1")
fmt.Println(mySessionData)
ret = chkDuplicate(mySessionData, "session1", "2")
fmt.Println(mySessionData)
ret = chkDuplicate(mySessionData, "session1", "3")
fmt.Println(mySessionData)
ret = chkDuplicate(mySessionData, "session1", "2")
fmt.Println(mySessionData)
ret = chkDuplicate(mySessionData, "session1", "4")
fmt.Println(mySessionData)
fmt.Println(ret)
delete(mySessionData, "session1")
fmt.Println(mySessionData)
}
func chkDuplicate(m map[string]map[string]bool, sess string, Reqno string) bool {
var found bool
val, found := m[sess][Reqno]
if !found {
fmt.Println(val)
valu := make(map[string]bool)
valu[Reqno] = true
m[sess] = valu
return !found
}
return !found
}
The problem I am facing is that the value is over written every time the function is called. What am I doing wrong here?