I have trying to write a function that will accept a string as an input and return another string in a slice but it not working. I have function that unmarshal udp response to a type AuthVectors and then append the type struct data into an array authVecSlice. Below is the code
type AuthVectors struct { MessageType string `json:"message_type"` IMSI string `json:"IMSI"` RAND string `json:"RAND"` XRES string `json:"XRES"` AUTN string `json:"AUTN"` } var imsistr = "244340000000011" var authVecSlice = make([]AuthVectors, 10) func getAuthVec() { hostName := "localhost" portNum := "4343" service := hostName + ":" + portNum RemoteAddr, err := net.ResolveUDPAddr("udp", service) conn, err := net.DialUDP("udp", nil, RemoteAddr) if err != nil { log.Fatal(err) } defer conn.Close() // write a message to server _, err = conn.Write([]byte(imsistr)) if err != nil { log.Println(err) } // receive message from server buffer := make([]byte, 1024) i, _, err := conn.ReadFromUDP(buffer) checkError(err) err = json.Unmarshal(buffer[:i], &avs) if err != nil { panic(err) } authVecSlice = append(authVecSlice, avs) fmt.Println(authVecSlice) } The array out put is as follows [{AUTN_PARAM 244340000000011 C98FC7C6DE9E1351A397D0AF99B6E890 D857E39D57E5E6BE 8000...} {AUTN_PARAM 244340000000011 211B025CB1CC8EE9CBB5FC7F56C1506B C9396A5AC7E68D62 31D...} {AUTN_PARAM 244340000000011 7D2A8C6E6B5CA243278987DFAF7FC829 C9EE960D0F5A501A 7E8..} ...] I would like to use the field RAND to search and return the corresponding XRES in the array. For example in the array is {AUTN_PARAM 244340000000011 C98FC7C6DE9E1351A397D0AF99B6E890 D857E39D57E5E6BE 8000...} I would like to use the RAND value for example C98FC7C6DE9E1351A397D0AF99B6E890 as a search string and return its corresponding XRES D857E39D57E5E6BE. func getXRES(a []AuthVectors, x interface{}) (xres string) { for _, v := range a { if x == v { xres := v.XRES return xres } } return "Item not found" } The problem for me is that the array value do not have key to be use to perform the search. All example I found involves the use of key to fetch the values. Any help -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b653494b-83c3-438c-bf42-170f526bd369%40googlegroups.com.