Go語言提供了對base64編碼/解碼的內置支持,並且具有可用於使用base64包對給定數據執行操作的函數。該軟件包提供了DecodeString()函數,該函數用於將base64字符串解碼為純文本格式。它支持使用標準和URL兼容的base64標準進行解碼。
用法:
func (enc *Encoding) DecodeString(s string) ([]byte, error)
解碼器使用的編碼類型有4種變化:
- StdEncoding:它是RFC 4648標準定義的要使用的標準編碼。
- RawStdEncoding:它是RFC 4648標準定義的要使用的標準編碼,隻是省略了填充字符。
- URLEncoding:它是RFC 4648標準所定義的URL編碼。它通常用於編碼URL和文件名。
- RawURLEncoding:它是RFC 4648標準所定義的URL編碼。它通常用於編碼URL和文件名,隻是省略了填充字符。
返回值:它返回由給定的base64字符串表示的字節。
以下示例程序旨在說明DecodeString()函數:
範例1:
// 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))
}
輸出:
Decoded Bytes:[71 101 101 107 115 102 111 114 71 101 101 107 115] Decoded String:GeeksforGeeks
範例2:
// Golang program to illustrate
// the base64.DecodeString() Function
package main
import (
"encoding/base64"
"fmt"
)
func main() {
// taking a string
givenString:= "aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv"
// using the function
decodedString, err:= base64.URLEncoding.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))
}
輸出:
Decoded Bytes:[104 116 116 112 115 58 47 47 119 119 119 46 103 101 101 107 115 102 111 114 103 101 101 107 115 46 111 114 103 47]
Decoded String:https://www.geeksforgeeks.org/
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 base64.DecodeString() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。