GoLang base64_decode

is this article helpful? yes | no
GoLang replacement for PHP's base64_decode [Golang Play | edit | history]
// Go provides built-in support for [base64
// encoding/decoding](http://en.wikipedia.org/wiki/Base64).

package main

// This syntax imports the `encoding/base64` package with
// the `b64` name instead of the default `base64`. It'll
// save us some space below.
import b64 "encoding/base64"
import "fmt"

func main() {

    // Here's the `string` we'll encode/decode.
    data := "abc123!?$*&()'-=@~"

    // Go supports both standard and URL-compatible
    // base64. Here's how to encode using the standard
    // encoder. The encoder requires a `[]byte` so we
    // cast our `string` to that type.
    sEnc := b64.StdEncoding.EncodeToString([]byte(data))
    fmt.Println(sEnc)

    // Decoding may return an error, which you can check
    // if you don't already know the input to be
    // well-formed.
    sDec, _ := b64.StdEncoding.DecodeString(sEnc)
    fmt.Println(string(sDec))
    fmt.Println()

    // This encodes/decodes using a URL-compatible base64
    // format.
    uEnc := b64.URLEncoding.EncodeToString([]byte(data))
    fmt.Println(uEnc)
    uDec, _ := b64.URLEncoding.DecodeString(uEnc)
    fmt.Println(string(uDec))
}

PHP base64_decode

PHP original manual for base64_decode [ show | php.net ]

base64_decode

(PHP 4, PHP 5, PHP 7)

base64_decodeDecodes data encoded with MIME base64

Description

string base64_decode ( string $data [, bool $strict = FALSE ] )

Decodes a base64 encoded data.

Parameters

data

The encoded data.

strict

If the strict parameter is set to TRUE then the base64_decode() function will return FALSE if the input contains character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded.

Return Values

Returns the original data or FALSE on failure. The returned data may be binary.

Changelog

Version Description
5.2.0 strict added

Examples

Example #1 base64_decode() example

<?php
$str 
'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo 
base64_decode($str);
?>

The above example will output:

This is an encoded string

See Also