How do we enable cors policy in server side using golang ?
main.go
func main() {
defer config.CloseDatabaseConnection(db)
r := gin.Default()
dataRoutes := r.Group("api/item")
{
dataRoutes.GET("/", dataController.All)
dataRoutes.POST("/", dataController.Insert)
dataRoutes.GET("/:id", dataController.FindByID)
dataRoutes.PUT("/:id", dataController.Update)
dataRoutes.DELETE("/:id", dataController.Delete)
}
r.Run()
}
I found
func Cors(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=ascii")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers","Content-Type,access-control-allow-origin, access-control-allow-headers")
}
But I am not sure how we implement in the golang? I have use above code while doing with c#
but I am stuck in golang
while implementing it.