1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-21 19:13:20 +08:00

完善主程序

This commit is contained in:
fumiama
2021-12-28 21:50:10 +08:00
parent f0956ae742
commit 6ae8e88bd1
18 changed files with 499 additions and 14 deletions

32
helper/data.go Normal file
View File

@@ -0,0 +1,32 @@
package helper
import (
"unsafe"
)
// slice is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
//
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type slice struct {
data unsafe.Pointer
len int
cap int
}
// BytesToString 没有内存开销的转换
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringToBytes 没有内存开销的转换
func StringToBytes(s string) (b []byte) {
bh := (*slice)(unsafe.Pointer(&b))
sh := (*slice)(unsafe.Pointer(&s)) // 不要访问 sh.cap
bh.data = sh.data
bh.len = sh.len
bh.cap = sh.len
return b
}

15
helper/file.go Normal file
View File

@@ -0,0 +1,15 @@
package helper
import "os"
// IsExist 文件/路径存在
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// IsNotExist 文件/路径不存在
func IsNotExist(path string) bool {
_, err := os.Stat(path)
return err != nil && os.IsNotExist(err)
}