package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // get value of User-Agent header from HTTP request ua := r.Header.Get("User-Agent") fmt.Fprintf(w, "User-Agent: %s\n", ua) }) http.ListenAndServe(":8080", nil) }
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // set a custom header value in the HTTP response w.Header().Set("My-Custom-Header", "my-value") fmt.Fprintf(w, "My-Custom-Header set to 'my-value'\n") }) http.ListenAndServe(":8080", nil) }In this example, we use the Set() method of the Header property of the HTTP response object to set a custom header value in the response. We then use the fmt.Fprintf() method to write a message to the response. In both examples, we use the net/http package provided by Go to work with HTTP request headers.