Menu Close

Go – CanonicalMIMEHeaderKey Function in net/textproto package in Go

In this article, we will explore the CanonicalMIMEHeaderKey Function in net/textproto package in Go in detail, along with examples.


In the Go programming language, the ‘textproto’ package is a part of the standard library, designed to work with text-based protocols such as HTTP and SMTP. The package provides several functions that help parse, read, and write text-based protocol headers. One such function is CanonicalMIMEHeaderKey, which is used to convert MIME header field names to their canonical format.

In this article, we will discuss the CanonicalMIMEHeaderKey function of the ‘textproto’ package in Go, explore its use cases, and provide a working example to demonstrate its functionality.

What is CanonicalMIMEHeaderKey Function ?

CanonicalMIMEHeaderKey is a function that converts a MIME header field name to its canonical format. The canonical format consists of a hyphen-separated, title-cased ASCII representation of the field name. For example, “content-type” would be converted to “Content-Type”.

Function Signature

The function signature for CanonicalMIMEHeaderKey is as follows:

func CanonicalMIMEHeaderKey(s string) string

CanonicalMIMEHeaderKey accepts a single string parameter ‘s’, which represents the MIME header field name, and returns a string representing the canonical format.


To demonstrate the use of CanonicalMIMEHeaderKey, let’s create a simple Go program that accepts a MIME header field name as user input and prints its canonical format.

package main

import (

func main() {

	var input string

	fmt.Print("Enter a MIME header field name: ")


	canonical := textproto.CanonicalMIMEHeaderKey(input)

	fmt.Printf("Canonical format: %s\n", canonical)

In the example above, the program accepts a user input and stores it in the ‘input’ variable. The ‘CanonicalMIMEHeaderKey’ function is then called with ‘input’ as an argument, and the result is stored in the ‘canonical’ variable. Finally, the canonical format is printed to the console.


Enter a MIME header field name: content-type
Canonical format: Content-Type


The CanonicalMIMEHeaderKey function is an essential utility provided by the ‘textproto’ package in the Go standard library. It simplifies the process of working with text-based protocols by converting MIME header field names to their canonical format. Understanding how to use this function is crucial for developers working with HTTP, SMTP, and other text-based protocols in Go.

To check more Go related articles. Pls click given below link:[email protected]

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *