golang 常用工具包

这篇博客汇总了Golang编程中常用的工具包,包括convertoper、fileoper、otheroper和reflectoper。通过这篇文章,读者可以方便地查阅和学习这些工具包的使用。
摘要由CSDN通过智能技术生成

convertoper

package convertoper

import (
	"encoding/binary"
	"reflect"
	"unsafe"
)


//byte数组转 int32
func BytesToInt32(buf []byte) int32 {
   
	return int32(binary.BigEndian.Uint32(buf))
}
//byte数组转 int64
func BytesToInt64(buf []byte) int64 {
   
	return int64(binary.BigEndian.Uint64(buf))
}

//https://www.cnblogs.com/ghost240/p/3739785.html
//string 转换成byte[]
func StringTobytes(s string) []byte {
   
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{
   x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
}
func SliceToString(b []byte) (s string) {
   
	pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
	pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
	pstring.Data = pbytes.Data
	pstring.Len = pbytes.Len
	return
}

//利用反射来转切片为string
func SliceToStringByintefce(v interface{
   }) (s string) {
   
	/*
		if reflect.TypeOf(v).Kind()!=reflect.Slice {
			otheroper.ErrorMsg(errors.New("当前类型不是Slice"),2,"")
			return
		}
	*/

	return string(reflect.ValueOf(v).Bytes())
}

//https://www.cnblogs.com/Detector/p/9686443.html
//  字符首字母大写
func FirstUpper(str string) string {
   
	var upperStr string

	vv := []rune(str) // 后文有介绍
	for i := 0; i < len(vv); i++ {
   
		if i == 0 {
   
			if vv[i] >= 97 && vv[i] <= 122 {
    // 后文有介绍
				vv[i] -= 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {
   
				//fmt.Println("Not begins with lowercase letter,")
				return str
			}
		} else {
   
			upperStr += string(vv[i])
		}
	}
	return upperStr
}

fileoper

package fileoper

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"tools/otheroper"
)

//复制文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值