Created
August 7, 2012 11:06
-
-
Save manuelkiessling/3284568 to your computer and use it in GitHub Desktop.
Go map with pointers to structs / with structs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package webservice | |
import ( | |
"io" | |
"io/ioutil" | |
"net/http" | |
"encoding/json" | |
"github.com/bmizerany/pat" | |
"manuel.kiessling.net/multivariate/runner/types" | |
// "fmt" | |
) | |
var experiments = make(map[string]types.Experiment) | |
func AddExperiment(res http.ResponseWriter, req *http.Request) { | |
body, _ := ioutil.ReadAll(req.Body) | |
data := make(map[string]string) | |
json.Unmarshal(body, &data) | |
experiment := types.Experiment{} | |
experiment.Name = data["name"] | |
experiment.SetUrl(data["url"]) | |
experiments[data["url"]] = experiment | |
} | |
func GetExperiments(res http.ResponseWriter, req *http.Request) { | |
url := req.FormValue("url") | |
experiment := experiments[url] | |
if experiment.Name != "" { | |
io.WriteString(res, "OK: " + url + " " + experiment.Name) | |
} else { | |
io.WriteString(res, "FAIL: " + url) | |
} | |
} | |
func NewMux() (mux *pat.PatternServeMux) { | |
mux = pat.New() | |
mux.Get("/experiments/", http.HandlerFunc(GetExperiments)) | |
mux.Post("/experiments/", http.HandlerFunc(AddExperiment)) | |
return mux | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package webservice | |
import ( | |
"io" | |
"io/ioutil" | |
"net/http" | |
"encoding/json" | |
"github.com/bmizerany/pat" | |
"manuel.kiessling.net/multivariate/runner/types" | |
// "fmt" | |
) | |
var experiments = make(map[string]*(types.Experiment)) | |
func AddExperiment(res http.ResponseWriter, req *http.Request) { | |
body, _ := ioutil.ReadAll(req.Body) | |
data := make(map[string]string) | |
json.Unmarshal(body, &data) | |
experiment := new(types.Experiment) | |
experiment.Name = data["name"] | |
experiment.SetUrl(data["url"]) | |
experiments[data["url"]] = experiment | |
} | |
func GetExperiments(res http.ResponseWriter, req *http.Request) { | |
url := req.FormValue("url") | |
experiment := experiments[url] | |
if experiment != nil { | |
io.WriteString(res, "OK: " + url + " " + (*experiment).Name) | |
} else { | |
io.WriteString(res, "FAIL: " + url) | |
} | |
} | |
func NewMux() (mux *pat.PatternServeMux) { | |
mux = pat.New() | |
mux.Get("/experiments/", http.HandlerFunc(GetExperiments)) | |
mux.Post("/experiments/", http.HandlerFunc(AddExperiment)) | |
return mux | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment