Source file src/net/textproto/header.go

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package textproto
     6  
     7  // A MIMEHeader represents a MIME-style header mapping
     8  // keys to sets of values.
     9  type MIMEHeader map[string][]string
    10  
    11  // Add adds the key, value pair to the header.
    12  // It appends to any existing values associated with key.
    13  func (h MIMEHeader) Add(key, value string) {
    14  	key = CanonicalMIMEHeaderKey(key)
    15  	h[key] = append(h[key], value)
    16  }
    17  
    18  // Set sets the header entries associated with key to
    19  // the single element value. It replaces any existing
    20  // values associated with key.
    21  func (h MIMEHeader) Set(key, value string) {
    22  	h[CanonicalMIMEHeaderKey(key)] = []string{value}
    23  }
    24  
    25  // Get gets the first value associated with the given key.
    26  // It is case insensitive; [CanonicalMIMEHeaderKey] is used
    27  // to canonicalize the provided key.
    28  // If there are no values associated with the key, Get returns "".
    29  // To use non-canonical keys, access the map directly.
    30  func (h MIMEHeader) Get(key string) string {
    31  	if h == nil {
    32  		return ""
    33  	}
    34  	v := h[CanonicalMIMEHeaderKey(key)]
    35  	if len(v) == 0 {
    36  		return ""
    37  	}
    38  	return v[0]
    39  }
    40  
    41  // Values returns all values associated with the given key.
    42  // It is case insensitive; [CanonicalMIMEHeaderKey] is
    43  // used to canonicalize the provided key. To use non-canonical
    44  // keys, access the map directly.
    45  // The returned slice is not a copy.
    46  func (h MIMEHeader) Values(key string) []string {
    47  	if h == nil {
    48  		return nil
    49  	}
    50  	return h[CanonicalMIMEHeaderKey(key)]
    51  }
    52  
    53  // Del deletes the values associated with key.
    54  func (h MIMEHeader) Del(key string) {
    55  	delete(h, CanonicalMIMEHeaderKey(key))
    56  }
    57  

View as plain text