当前位置:网站首页>Golang []byte to file
Golang []byte to file
2022-06-10 14:52:00 【Deng_ Xian_ Sheng】
// Package byteToFile
// []byte to fs.file, reference embed
package byteToFile
import (
"io"
"io/fs"
"reflect"
"time"
)
// A file is a single file in the FS.
// It implements fs.FileInfo and fs.DirEntry.
type file struct {
name string
data string
hash [16]byte // truncated SHA256 hash
}
func (f *file) Name() string {
return f.name }
func (f *file) Size() int64 {
return int64(len(f.data)) }
func (f *file) ModTime() time.Time {
return time.Time{
} }
func (f *file) IsDir() bool {
return false }
func (f *file) Sys() any {
return nil }
func (f *file) Type() fs.FileMode {
return f.Mode().Type() }
func (f *file) Info() (fs.FileInfo, error) {
return f, nil }
func (f *file) Mode() fs.FileMode {
if f.IsDir() {
return fs.ModeDir | 0555
}
return 0444
}
// An openFile is a regular file open for reading.
type openFile struct {
f *file // the file itself
offset int64 // current read offset
}
func (f *openFile) Close() error {
return nil }
func (f *openFile) Stat() (fs.FileInfo, error) {
return f.f, nil }
// Read reads up to len(b) bytes from the File and stores them in b.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *openFile) Read(b []byte) (int, error) {
if f.offset >= int64(len(f.f.data)) {
return 0, io.EOF
}
if f.offset < 0 {
return 0, &fs.PathError{
Op: "read", Path: f.f.name, Err: fs.ErrInvalid}
}
n := copy(b, f.f.data[f.offset:])
f.offset += int64(n)
return n, nil
}
var (
_ io.Seeker = (*openFile)(nil)
_ fs.FileInfo = (*file)(nil)
)
func (f *openFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
// offset += 0
case 1:
offset += f.offset
case 2:
offset += int64(len(f.f.data))
}
if offset < 0 || offset > int64(len(f.f.data)) {
return 0, &fs.PathError{
Op: "seek", Path: f.f.name, Err: fs.ErrInvalid}
}
f.offset = offset
return offset, nil
}
func (f *openFile) Write(b []byte) (int, error) {
if reflect.ValueOf(f).IsNil() {
return 0, &fs.PathError{
Op: "write", Path: f.f.name, Err: fs.ErrInvalid}
}
f.f.data += string(b)
return len(b), nil
}
func New() *openFile {
return &openFile{
f: &file{
},
offset: 0,
}
}
package main
import (
"fmt"
"demo/byteToFile"
)
func main(){
data := []byte("I like Deng Wenyi ")
object := byteToFile.New()
_,err := object.Write(data)
if err != nil{
panic(err)
}
read := make([]byte,len(data))
_,err = object.Read(read)
if err != nil{
panic(err)
}
fmt.Println(string(read))
}
边栏推荐
- 初识RPC
- [discrete mathematics review series] IV. figure
- As a programmer, is it really that important for the underlying principles?
- . Net C Foundation (7): interface - how people interact with cats
- Binary tree and Figure 2
- CVPR 2022 | frame by frame motion representation of long video based on sequence contrast learning
- Blogger Confessions
- 【云原生 | Kubernetes篇】深入RC、RS、DaemonSet、StatefulSet(七)
- 洞察的力量
- 这个牛逼的低代码生成器,现在开源了!
猜你喜欢

How to implement the association between interfaces in JMeter?
![[original] poi 5 X xssf and HSSF use custom font colors](/img/fc/0d983205784f3c3118bf4d2a73f842.png)
[original] poi 5 X xssf and HSSF use custom font colors

利用 GDB 快速阅读 postgresql 的内核代码

Singleton pattern and special class design

Super practical operation! Calibration and registration of Kinect depth map and RGB camera for hands-on teaching
![[logodetection data set processing] (2) draw the label box of the training set picture](/img/66/6c19b80b99d1e3ce50bac439e0e903.jpg)
[logodetection data set processing] (2) draw the label box of the training set picture

Flutter Icon Stack LIsttitle... Learning summary 3

数据库创建触发器的问题

【原创】POI 5.x XSSF和HSSF使用自定义字体颜色

Hutool使用总结(VIP典藏版)
随机推荐
Basic concept of data warehouse
Super practical operation! Calibration and registration of Kinect depth map and RGB camera for hands-on teaching
AUTOCAD——设置文字间距与行距
Golang []byte 转 File
小程序警告:Now you can provide attr `wx:key` for a `wx:for` to improve performance.
3、再遇HandyControl之窗体
作为程序员,对于底层原理真的有那么重要吗?
Generate a dataset of training vectors for doc2vec
产品开发的早期阶段,是选择开发app还是小程序?
Insight technology was selected into the "Aijian · privacy computing manufacturer panorama report" and was rated as a representative manufacturer of financial solutions
4、再遇Panuon.UI.Silver之窗体标题栏
BigDecimal 去除末尾多余的0
Cell asynchronously invokes method change records
Kubernetes 1.24: 避免为 Services 分配 IP 地址时发生冲突
CANN的接口调用流程概述
Docker deploys a redis cluster
二分查找详解
洞察的力量
Shutter wrap button bottomnavigationbar learning summary 4
Collision detection unity experiment code