def<\/td> ZGVm<\/td> ZGVm<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nNow let’s consider both cases.<\/p>\n\n\n\n
When the concatenation is sent without padding<\/strong><\/p>\n\n\n\nIn this case, the concatenated Base64 string will be<\/p>\n\n\n\n
YQYmMZGVm<\/code><\/pre>\n\n\n\nTry decoding it and you will get the final string as below which is incorrect<\/p>\n\n\n\n
a&1<\/code><\/pre>\n\n\n\nWhen the concatenation is sent with padding<\/strong><\/p>\n\n\n\nIn this case, the concatenated Base64 string will be<\/p>\n\n\n\n
YQ==YmM=ZGVm<\/code><\/pre>\n\n\n\nTry decoding it in groups of 4 characters and you will get the final string as below which is correct<\/p>\n\n\n\n
abcdef<\/code><\/pre>\n\n\n\nNow again the question that comes to mind is why do you need to concatenate multiple base64 encoded strings. The answer is that it is always good in cases where there is streaming data and you want to send the base64 encoded data as it comes. For example, buffering of video.<\/p>\n\n\n\n
So that is why padding is encouraged though not absolutely not necessary in all cases. Now let’s see an example of Raw Std Encoding<\/p>\n\n\n\n
package main\n\nimport (\n\t\"encoding\/base64\"\n\t\"fmt\"\n\t\"log\"\n)\n\nfunc main() {\n\tsample := \"a@\"\n\tencodedStringStdEncoding := base64.StdEncoding.EncodeToString([]byte(sample))\n\tfmt.Printf(\"STD Encoding: %s\\n\", encodedStringStdEncoding)\n\n\tencodedStringRawStdEncoding := base64.RawStdEncoding.EncodeToString([]byte(sample))\n\tfmt.Printf(\"Raw STD Encoding: %s\\n\", encodedStringRawStdEncoding)\n\n\toriginalStringBytes, err := base64.RawStdEncoding.DecodeString(encodedStringRawStdEncoding)\n\tif err != nil {\n\t\tlog.Fatalf(\"Some error occured during base64 decode. Error %s\", err.Error())\n\t}\n\tfmt.Println(string(originalStringBytes))\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nSTD Encoding: YUA=\nRaw STD Encoding: YUA\na@<\/code><\/pre>\n\n\n\nIn the above example we are printing both the result of both StdEncoding<\/strong> as well as RawStdEncoding<\/strong><\/p>\n\n\n\nencodedStringStdEncoding := base64.StdEncoding.EncodeToString([]byte(sample))\nfmt.Printf(\"STD Encoding: %s\\n\", encodedStringStdEncoding)\n\nencodedStringRawStdEncoding := base64.RawStdEncoding.EncodeToString([]byte(sample))\nfmt.Printf(\"Raw STD Encoding: %s\\n\", encodedStringRawStdEncoding)<\/code><\/pre>\n\n\n\nNotice the difference in output<\/p>\n\n\n\n
For STD encoding it prints<\/p>\n\n\n\n
YUA=<\/code><\/pre>\n\n\n\nwhile for Raw STD encoding, it prints<\/p>\n\n\n\n
YUA<\/code><\/pre>\n\n\n\nAs you can notice that RAW STD Encoding omits padding characters.<\/p>\n\n\n\n
<\/span>RawURLEncoding<\/strong><\/span><\/h1>\n\n\n\nThis is also the same as URL encoding just that it omits padding characters. So it is unpadded STD base64 encoding.<\/p>\n\n\n\n
package main\nimport (\n \"encoding\/base64\"\n \"fmt\"\n \"log\"\n)\nfunc main() {\n sample := \"a@\"\n encodedStringURLEncoding := base64.URLEncoding.EncodeToString([]byte(sample))\n fmt.Printf(\"URL Encoding: %s\\n\", encodedStringURLEncoding)\n\n encodedStringRawURLEncoding := base64.RawURLEncoding.EncodeToString([]byte(sample))\n fmt.Printf(\"Raw URL Encoding: %s\\n\", encodedStringRawURLEncoding)\n\n originalStringBytes, err := base64.RawStdEncoding.DecodeString(encodedStringRawURLEncoding)\n if err != nil {\n log.Fatalf(\"Some error occured during base64 decode. Error %s\", err.Error())\n }\n fmt.Println(string(originalStringBytes))\n}<\/code><\/pre>\n\n\n\n\u00a0Output<\/strong><\/p>\n\n\n\nURL Encoding: YUA=\nRaw URL Encoding: YUA\na@<\/code><\/pre>\n\n\n\nIn the above example, we are again printing the result of both URL encoding and as well as RAW URL encoding and you can notice that padding character ‘=’ <\/strong>is omitted in the Raw URL encoding.<\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewStdEncodingURLEncodingRawStdEncodingRawURLEncoding Overview Golang provides an encoding\/base64 package that can be used to encode strings to base64 and decode the base64 encoded string back to the original string. https:\/\/golang.org\/pkg\/encoding\/base64\/ Go…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-4796","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\n
Base64 encoding\/Decoding in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n