| |
下面是一个将Base64字符串数组作为参数传入的Go函数示例。该函数会解码每个Base64字符串,拼接它们的原始数据,然后重新编码为一个Base64字符串。
package main
import (
"encoding/base64"
"fmt"
"log"
)
// 拼接Base64字符串数组的函数
func concatenateBase64(base64Parts []string) (string, error) {
var combinedData []byte
for _, part := range base64Parts {
// 解码Base64字符串
decodedPart, err := base64.StdEncoding.DecodeString(part)
if err != nil {
return "", fmt.Errorf("解码Base64字符串 %s 时出错: %v", part, err)
}
// 合并解码后的数据
combinedData = append(combinedData, decodedPart...)
}
// 重新编码为Base64
combinedBase64 := base64.StdEncoding.EncodeToString(combinedData)
return combinedBase64, nil
}
func main() {
// 示例Base64字符串数组
base64Parts := []string{
"SGVsbG8=", // Hello
"V29ybGQ=", // World
}
// 调用函数
result, err := concatenateBase64(base64Parts)
if err != nil {
log.Fatalf("拼接Base64字符串时出错: %v", err)
}
fmt.Println("重新编码后的Base64字符串:", result)
}