Answer the question
In order to leave comments, you need to log in
How to correctly declare a function with a response-message of the Go-server?
What is the right way to make a separate function sending a response by the Go-server?
w.Header().Add("keyValue", "value") // Специальный заголовок HTTP-ответа сервера.
var dataResp = "Произошёл сбой."
var formatTextRespon, _ = template.New("dataRespons").Parse("{{ .}}")
formatTextRespon.Execute(w, dataResp)
return
Answer the question
In order to leave comments, you need to log in
well, if it's a simple option,
you can do this
func main() {
http.HandleFunc("/", myResponseFunction) //set up a function that will work on a request
}
// myResponseFunction, which will return on request /
func myResponseFunction(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8") // Set header . Add, Del, Get Set methods for headers
t, err := template.ParseFiles("template.html")
if err != nil {
fmt.Fprintf(w, "Unable to load template")
}
user := User {
Id : 1,
Name: "Basta",
Email: "[email protected]",
Phone: "Basta009"
}
t.Execute(w, user) //execute template
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question