1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package main import ( "net/http" "github.com/rs/cors" ) func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) http.ListenAndServe(":8080", c.Handler(handler)) }