base64 Package in Golang Last Updated : 22 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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. Function Description NewDecoder This function is used to construct a new base64 stream decoder. NewEncoder This 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 } Method Description func NewEncoding This 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) Decode This 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) DecodeString This method is used to return the bytes represented by the base64 string s. func (*Encoding) DecodedLen This method is used to return the maximum length in bytes of the decoded data corresponding to n bytes of base64-encoded data. func (*Encoding) Encode This method is used to encodes src using the encoding enc, writing EncodedLen(len(src)) bytes to dst. func (*Encoding) EncodeToString This method is used to return the base64 encoding of src. func (*Encoding) EncodedLen This method is used to return the length in bytes of the base64 encoding of an input buffer of length n. func (Encoding) Strict This method is used to create a new encoding identical to enc except with strict decoding enabled. func (Encoding) WithPadding This method is used to create a new encoding identical to enc except with a specified padding character, or NoPadding to disable padding. Example 1: Go // 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: Go // 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== Comment More infoAdvertise with us Next Article strconv package in Golang A ankita_saini Follow Improve Article Tags : Go Language Golang-Misc Similar Reads Packages in Golang Packages are the most powerful part of the Go language. The purpose of a package is to design and maintain a large number of programs by grouping related features together into single units so that they can be easy to maintain and understand and independent of the other package programs. This modula 5 min read fmt Package in GoLang Prerequisite: Packages in GoLang and Import in GoLang Technically defining, a package is essentially a container of source code for some specific purpose. Packages are very essential as in all programs ranging from the most basic programs to high-level complex codes, these are used. A package ensur 13 min read filepath Package in Golang Go language provides inbuilt support for implementing utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths with the help of the filepath package. This package uses either forward slashes or backslashes (depending on the operating sy 3 min read string package in Golang Go language provides a string package that holds different types of functions to manipulate UTF-8 encoded strings. To access the function of the string package you need to import a string package in your program with the help of the import keyword.  FunctionDescriptionfunc CompareThis function is u 8 min read strconv package in Golang Go language provides a strconv package that implements conversions to and from string representations of basic data types. To access the functions of the strconv package you need to import the strconv package in your program with the help of the import keyword. .strconv-golang-table { border-collaps 5 min read How to Create Your Own Package in Golang? Go language is a high-level programming language developed at Google Inc. A high-level language is in simple words, the programming language category created by humans for human understanding. Before we jump onto high terminologies as packages, modules, functions, etc. let's write the most basic pro 7 min read Like