title: Proper way to add padding to byte slice in golang?
body: <p>I'm trying to encrypt some data in go but it's hardly ever the correct <code>cipher.BlockSize</code>.</p>
  
  <p>Is there a "built-in" way to add padding or should I be using a function to add it manually?</p>
  
  <p>This is my solution now:</p>
  
  <pre><code>// encrypt() encrypts the message, but sometimes the 
  // message isn't the proper length, so we add padding.
  func encrypt(msg []byte, key []byte) []byte {        
    cipher, err := aes.NewCipher(key)                  
    if err != nil {                                    
      log.Fatal(err)                                   
    }                                                  
  
    if len(msg) &lt; cipher.BlockSize() {                 
      var endLength = cipher.BlockSize() - len(msg)    
      ending := make([]byte, endLength, endLength)     
      msg = append(msg[:], ending[:]...)               
      cipher.Encrypt(msg, msg)                         
    } else {                                           
      var endLength = len(msg) % cipher.BlockSize()    
      ending := make([]byte, endLength, endLength)     
      msg = append(msg[:], ending[:]...)               
      cipher.Encrypt(msg, msg)                         
    }                                                  
    return msg                                         
  }                                                    
  </code></pre>