1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package main
import ( "encoding/base64" "fmt" "os" )
func main() { file, err := os.Open("jw.png") if err != nil { fmt.Printf("os open file error %v\n", err) return } defer file.Close()
// 获取文件的统计相关信息 info, err := file.Stat() if err != nil { fmt.Printf("file stat error %v\n", err) return }
srcBuffer := make([]byte, info.Size()) n, err := file.Read(srcBuffer) if err != nil { fmt.Printf("file read error %v\n", err) return }
// 转base64 base64Str := base64.StdEncoding.EncodeToString(srcBuffer[:n]) fmt.Printf("Base64: %v\n", base64Str) }
|