项目实践总结
文件打包程序
- 开发语言:golang
- 开发工具:liteide
- 运行环境:Linux32位
1.通过首地址指针获取结构体数据
import ( "unsafe" ) dataBytes := (*[568]byte)(unsafe.Pointer(&snap))
2.数组越界异常处理
func memcpyByte(dst, src []byte) { defer func() { recover() }() size := len(src) for i := 0; i < size; i++ { dst[i] = src[i] } }
3.编码格式转换
import ( "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" ) //UTF8转GBK func UTF8ToGBK(s []byte) []byte { reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder()) d, e := ioutil.ReadAll(reader) if e != nil { return nil } return d } //GBK转UTF8 func GBKToUTF8(s []byte) []byte { reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder()) d, e := ioutil.ReadAll(reader) if e != nil { return nil } return d }
TCP传输接受服务器
文件打包程序
- 开发语言:golang
- 开发工具:liteide
- 运行环境:Linux32位
1.文件处理
import ( "os" "sync" ) //判断文件存在 func Exist(fileName string) (fExist bool) { //判断ts_data中存在断点数据 _, err := os.Stat(fileName) if err == nil || os.IsExist(err) { fExist = true } return fExist } //创建地址 func MakeDir(dir string) { _, err := os.Stat(dir) if err != nil { os.MkdirAll(dir, os.ModePerm) } } //打开文件 func OpenFile(fileName string) (fout *os.File, err error) { fout, err = os.OpenFile(fileName, os.O_RDWR, 0666) //os.O_CREATE|os.O_RDWR||os.O_APPEND return fout, err } //打开文件(创建) func OpenFileCreate(fileName string) (fout *os.File, err error) { fout, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0666) return fout, err } //移除文件 func RemoveFile(filename string, lock *sync.RWMutex) { lock.Lock() os.Remove(filename) lock.Unlock() }
2.自定义字符输出
import ( "fmt" "os" ) //printf func CodePrintf(str string, args ...interface{}) { fmt.Printf(str, args...) } //println func CodePrintln(args ...interface{}) { fmt.Println(args...) } //Fprintf func CodeFPrintf(str string, args ...interface{}) { fmt.Fprintf(os.Stderr, str, args...) } //Sprintf func CodeSPrintf(str string, args ...interface{}) string { return fmt.Sprintf(str, args...) }
3.字符串处理
import ( "bytes" "path" "strings" ) //file_name处理 空白行处理 func DealFilename(filename string) (file_name string) { return strings.TrimSpace(filename) } //处理key 后面接的0 func DealBuf(key []byte) []byte { index := bytes.IndexByte(key, 0) if index == -1 { return key } return key[:index] } //分离key 获得 devid设备号 date日期 name文件名 ext扩展名 func SplitKey(key string) (devid, date, name, ext, dir string, part_length int) { //fmt.Println(key) part := strings.SplitN(key, "/", -1) part_length = len(part) //fmt.Println(part_length) devid = part[0] if part_length > 1 { date = part[part_length-2 : part_length-1][0] name, ext = SplitFilename(part[part_length-1:][0]) //拼接除去name.ext的地址 for k, v := range part { if k != (part_length - 2) { dir = dir + v + "/" } else { dir = dir + v break } } } return } //分离 name.ext 获得 name文件名 ext扩展名 func SplitFilename(file_name string) (name, ext string) { filenameWithSuffix := path.Base(file_name) ext = path.Ext(filenameWithSuffix) name = strings.TrimSuffix(filenameWithSuffix, ext) return } //去掉 .temp func DltTemp(filename string) string { //return strings.TrimRight(filename, ".temp") //删除时会出现问题 txt.temp -> tx return strings.Trim(strings.TrimRight(filename, "temp"), ".") } //转换路径将 /转换为\\ func ChangePath(file_path string) string { return strings.Replace(file_path, "/", "\\", -1) }
4.类型转换
import ( "encoding/binary" "strconv" ) //字符串string - 整型int func StringToInt(str string) (num int) { num, err := strconv.Atoi(str) CheckError(err) return } //整型int - 字符串string func IntToString(num int) string { return strconv.Itoa(num) } //整型int32 - byte数组 func Int32ToBytes(i int32) []byte { var buf = make([]byte, 4) // int64 is 8 byte int32 4 byte int16 2 byte int8 byte binary.LittleEndian.PutUint32(buf, uint32(i)) return buf } //byte数组 - 整型int32 func BytesToInt32(buf []byte) int32 { return int32(binary.LittleEndian.Uint32(buf)) } //byte数组 - 整型int16 func BytesToint16(buf []byte) int16 { return int16(binary.LittleEndian.Uint16(buf)) } //byte数组 - 整型uint8 func BytesToUint8(buf []byte) uint8 { return buf[0] } //byte数组 - 整型int8 func BytesToInt8(buf []byte) int8 { return int8(buf[0]) } //string字符串-布尔类型bool func StringToBool(str string) (boolean bool) { boolean, err := strconv.ParseBool(str) CheckError(err) return boolean }