V
V
Vadim Rublev2020-11-28 17:54:29
go
Vadim Rublev, 2020-11-28 17:54:29

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

Here "keyValue", "value" must be passed in a variable. How to do it? And what is the data type of the variable?
And generally speaking...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Klars, 2020-12-01
@Klars

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 question

Ask a Question

731 491 924 answers to any question