I want to convert URL parameters from a string, to the correct data type and store it in a map. I believe the type should be []interface, to store a list of multiple data types.
I am creating a key-value store, but at the moment, it only stores strings. I have read up on GOB to convert the data into bytes, but don't understand it.
I have tried type assertion, however, I don't believe in my case this will work as the data is coming from the URL which by default is already a string. How do I detect whether the entry is a string, int, boolean etc?
Map:
var data map[string][]string
handleFunc in the main func which calls the handleCreate func
r.HandleFunc("/insert/{key}/{value}", handleCreate).Methods("POST")
handleFunc function
func handleCreate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
k := params["key"]
v := params["value"]
data[k] = append(data[k], v)
fmt.Fprintf(w, "KEY: %v: VALUE: %v was inserted into the key-value store successfully", k, v)
}
A link to the full code:
https://github.com/RipRoss/TrainingProjects/blob/master/main.go
The expected result is to store the value given in the URL as what it is intended for. For example /dog/Zeus would store Zeus as a string with key 'dog' and /cash/1000 would store 1000 as type int with key 'cash'