parent
cc94e9092b
commit
12fcbd8d19
@ -0,0 +1,4 @@
|
||||
package file
|
||||
|
||||
type File struct {
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package file
|
||||
|
||||
var file File
|
@ -0,0 +1,37 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"os"
|
||||
)
|
||||
|
||||
func (fl *File) ByteToBase64(b []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Base64ToByte base64 string to byte
|
||||
// baseStr parameter is base64 string
|
||||
func (fl *File) Base64ToByte(baseStr string) ([]byte, error) {
|
||||
return base64.StdEncoding.DecodeString(string(baseStr))
|
||||
}
|
||||
|
||||
// ByteToFile The byte array is converted into a file and landed
|
||||
// The parameter b is the bate array filePath file path
|
||||
func (fl *File) ByteToFile(b []byte, filePath string) error {
|
||||
//Open file Create a file if there is no file
|
||||
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = file.Write(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(file)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFile_ByteToBase64(t *testing.T) {
|
||||
byteStr := []byte("hello world!")
|
||||
base64Str := file.ByteToBase64(byteStr)
|
||||
byteresult, _ := file.Base64ToByte(base64Str)
|
||||
fmt.Println(base64Str)
|
||||
fmt.Println(byteresult)
|
||||
}
|
Loading…
Reference in new issue