Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

base64 Package in Golang

  • Last Updated : 22 Jun, 2020

Go language provides inbuilt support for base64 encoding/decoding and has functions that could be used to perform operations on the given data using the base64 package.

FunctionDescription
NewDecoderThis function is used to construct a new base64 stream decoder.
NewEncoderThis function is used to return a new base64 stream encoder.

type Encoding: An Encoding is a radix 64 encoding/decoding scheme which is defined by a 64-character alphabet. The most common encoding is the “base64” encoding defined in RFC 4648 and it is used in MIME (RFC 2045) and PEM (RFC 1421). RFC 4648 also defines an alternate encoding, which is the standard encoding with – and _ substituted for + and /.

type Encoding struct {
    // It holds filtered or unexported fields
}
MethodDescription
func NewEncodingThis function is used to return a new padded Encoding defined by the given alphabet, which must be a 64-byte string that does not contain the padding character or CR / LF (‘\r’, ‘\n’).
func(*Encoding) DecodeThis method is used to decodes src using the encoding end. This method will writes at most DecodedLen(len(src)) bytes to dst and returns the number of bytes written. If src contains invalid base64 data, then this method will return the number of bytes successfully written and CorruptInputError. Here, new line characters (\r and \n) are ignored.
func (*Encoding) DecodeStringThis method is used to return the bytes represented by the base64 string s.
func (*Encoding) DecodedLenThis method is used to return the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data.
func (*Encoding) EncodeThis method is used to encodes src using the encoding enc, writing EncodedLen(len(src)) bytes to dst.
func (*Encoding) EncodeToStringThis method is used to return the base64 encoding of src.
func (*Encoding) EncodedLenThis method is used to return the length in bytes of the base64 encoding of an input buffer of length n.
func (Encoding) StrictThis method is used to create a new encoding identical to enc except with strict decoding enabled.
func (Encoding) WithPaddingThis method is used to create a new encoding identical to enc except with a specified padding character, or NoPadding to disable padding.

Example 1:




// Golang program to illustrate
// the base64.DecodeString() Function
package main
  
import (
    "encoding/base64"
    "fmt"
)
  
func main() {
  
    // Taking a string
    givenString := "R2Vla3Nmb3JHZWVrcw=="
  
    // using the function
    decodedString, err := base64.StdEncoding.DecodeString(givenString)
    if err != nil {
        fmt.Println("Error Found:", err)
        return
    }
  
    fmt.Print("Decoded Bytes: ")
    fmt.Println(decodedString)
  
    fmt.Print("Decoded String: ")
    fmt.Println(string(decodedString))
}

Output:

Decoded Bytes: [71 101 101 107 115 102 111 114 71 101 101 107 115]
Decoded String: GeeksforGeeks

Example 2:




// Golang program to illustrate
// the base64.NewEncoder() function
package main
  
import (
    "encoding/base64"
    "fmt"
)
  
func main() {
  
    // Input string
    giveninput := []byte("GeeksforGeeks")
  
    // Using EncodeToString() function
    // Encode the given string
    result := base64.StdEncoding.EncodeToString(giveninput)
    fmt.Println("Encoded string: ", result)
}

Output:

Encoded string:  R2Vla3Nmb3JHZWVrcw==



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!