package lib import ( "fmt" "math" "strconv" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" "legu.airobot/pb" ) func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) { any, err := anypb.New(rsp) if err != nil { fmt.Printf("Any New %s.%s %v", msg.MainType, msg.SubType, err) return } msg.Data = any return true } func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) { err := msg.Data.UnmarshalTo(req) if err != nil { fmt.Printf("UnmarshalTo %s.%s %v\n", msg.MainType, msg.SubType, err) return } return true } func SubStr(str string, start int, length int) (result string) { s := []rune(str) total := len(s) if total == 0 { return } // 允许从尾部开始计算 if start < 0 { start = total + start if start < 0 { return } } if start > total { return } // 到末尾 if length < 0 { length = total } end := start + length if end > total { result = string(s[start:]) } else { result = string(s[start:end]) } return } func DeleteString(list []string, ele string) []string { result := make([]string, 0) for _, v := range list { if v != ele { result = append(result, v) } } return result } func ConvertFileSize(size int64) string { if size == 0 { return "0" } kb := float64(size) / float64(1024) if kb < 1024 { f := math.Ceil(kb) return strconv.Itoa(int(f)) + " KB" } mb := kb / float64(1024) if mb < 1024 { f := math.Ceil(mb) return strconv.Itoa(int(f)) + " MB" } gb := mb / float64(1024) if gb < 1024 { f := math.Ceil(mb) return strconv.Itoa(int(f)) + " G" } t := gb / float64(1024) if t < 1024 { f := math.Ceil(mb) return strconv.Itoa(int(f)) + " T" } if t >= 1024 { return "VeryBig" } return "0" }