package main import ( "fmt" "net/mail" "net/textproto" ) func main() { msg := `From: [email protected] To: [email protected] Subject: Test message Hello! This is a test message.` header, err := mail.ReadMessage(strings.NewReader(msg)) if err != nil { panic(err) } fmt.Printf("From: %s\n", header.Header.Get("From")) fmt.Printf("To: %s\n", header.Header.Get("To")) fmt.Printf("Subject: %s\n", header.Header.Get("Subject")) fmt.Printf("Body: %s\n", header.Body) }
package main import ( "fmt" "net/http" "net/textproto" ) func handler(w http.ResponseWriter, req *http.Request) { headers := textproto.MIMEHeader{} headers.Add("Foo", "bar") headers.Add("X-Baz", "qux") for key, values := range headers { for _, value := range values { w.Header().Add(key, value) } } w.WriteHeader(200) fmt.Fprintf(w, "Hello, world!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }In this example, we create a custom MIME-style header using the textproto.MIMEHeader type, and then add some key-value pairs to it. We then use the standard library http.ResponseWriter to write the header fields to the HTTP response using the w.Header().Add function.