github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/base64.md (about) 1 --- 2 title: base64 functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 9 ## `base64.Encode` 10 11 Encode data as a Base64 string. Specifically, this uses the standard Base64 encoding as defined in [RFC4648 §4](https://tools.ietf.org/html/rfc4648#section-4) (and _not_ the URL-safe encoding). 12 13 _Added in gomplate [v1.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.8.0)_ 14 ### Usage 15 16 ``` 17 base64.Encode input 18 ``` 19 ``` 20 input | base64.Encode 21 ``` 22 23 ### Arguments 24 25 | name | description | 26 |------|-------------| 27 | `input` | _(required)_ The data to encode. Can be a string, a byte array, or a buffer. Other types will be converted to strings first. | 28 29 ### Examples 30 31 ```console 32 $ gomplate -i '{{ base64.Encode "hello world" }}' 33 aGVsbG8gd29ybGQ= 34 ``` 35 ```console 36 $ gomplate -i '{{ "hello world" | base64.Encode }}' 37 aGVsbG8gd29ybGQ= 38 ``` 39 40 ## `base64.Decode` 41 42 Decode a Base64 string. This supports both standard ([RFC4648 §4](https://tools.ietf.org/html/rfc4648#section-4)) and URL-safe ([RFC4648 §5](https://tools.ietf.org/html/rfc4648#section-5)) encodings. 43 44 This function outputs the data as a string, so it may not be appropriate 45 for decoding binary data. Use [`base64.DecodeBytes`](#base64.DecodeBytes) 46 for binary data. 47 48 _Added in gomplate [v1.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.8.0)_ 49 ### Usage 50 51 ``` 52 base64.Decode input 53 ``` 54 ``` 55 input | base64.Decode 56 ``` 57 58 ### Arguments 59 60 | name | description | 61 |------|-------------| 62 | `input` | _(required)_ The base64 string to decode | 63 64 ### Examples 65 66 ```console 67 $ gomplate -i '{{ base64.Decode "aGVsbG8gd29ybGQ=" }}' 68 hello world 69 ``` 70 ```console 71 $ gomplate -i '{{ "aGVsbG8gd29ybGQ=" | base64.Decode }}' 72 hello world 73 ``` 74 75 ## `base64.DecodeBytes` 76 77 Decode a Base64 string. This supports both standard ([RFC4648 §4](https://tools.ietf.org/html/rfc4648#section-4)) and URL-safe ([RFC4648 §5](https://tools.ietf.org/html/rfc4648#section-5)) encodings. 78 79 This function outputs the data as a byte array, so it's most useful for 80 outputting binary data that will be processed further. 81 Use [`base64.Decode`](#base64.Decode) to output a plain string. 82 83 _Added in gomplate [v3.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.8.0)_ 84 ### Usage 85 86 ``` 87 base64.DecodeBytes input 88 ``` 89 90 ### Arguments 91 92 | name | description | 93 |------|-------------| 94 | `input` | _(required)_ The base64 string to decode | 95 96 ### Examples 97 98 ```console 99 $ gomplate -i '{{ base64.DecodeBytes "aGVsbG8gd29ybGQ=" }}' 100 [104 101 108 108 111 32 119 111 114 108 100] 101 ``` 102 ```console 103 $ gomplate -i '{{ "aGVsbG8gd29ybGQ=" | base64.DecodeBytes | conv.ToString }}' 104 hello world 105 ```