lib

package module
v0.0.0-...-eb9e4ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2024 License: GPL-3.0 Imports: 21 Imported by: 1

README

lib

Go Reference Go Report Card

This is a Go language library that provides some useful functions to facilitate rapid development.

中文自述在这里.

Installation

To use this library, you need to install it in your project. You can install it by running the following command:

go get github.com/limitcool/lib

Function List

Debugging and Environment
  • SetDebugMode(debugFunction func()): Set the debug mode based on the environment variable and execute a debug function.
Slice Manipulation
  • InSlice(element T, Slice []T) bool: Check if an element exists in a slice.
  • Unique(slice []T) []T: Remove duplicate elements from a slice.
  • Push(slice []T, element T) []T: Add an element to the end of a slice.
  • Pop(slice []T) ([]T, T): Remove and return the last element from a slice.
  • PopFront(slice []T) ([]T, T): Remove and return the first element from a slice.
  • Exclude(slice []T, element T) []T: Return a slice without the specified element.
  • Reverse(s []T) []T: Reverse the order of elements in a slice.
Map Manipulation
  • Keys(m map[K]V) []K: Return a slice containing all the keys in a map.
Type Conversion
  • IntToBool(a T) bool: Convert an integer to a boolean value.
  • ConvertAnyToInterface(anyValue *any.Any) (interface{}, error): Convert Google's protobuf any.Any type to Go's interface{} type.
  • ConvertInterfaceToAny(v interface{}) (*any.Any, error): Convert Go's interface{} type to Google's protobuf any.Any type.
  • MustConvertInterfaceToAny(v interface{}) *any.Any: Similar to ConvertInterfaceToAny, but ignores errors.
  • MustConvertAnyToInterface(anyValue *any.Any) interface{}: Similar to ConvertAnyToInterface, but ignores errors.
File and Folder Operations
  • FileExists(filename string) bool: Check if a file exists.
  • FolderExists(foldername string) bool: Check if a folder exists.
  • FilesInFolder(dir, filename string) ([]string, error): Return a list of file paths containing the specified file in the given directory.
  • ReadFile(filename string) (string, error): Read the content of a file and return it.
  • CopyDir(src, dst, skip string) error: Copy a directory from the source path to the destination path, skipping files with a specific suffix.
  • CopyDirHasSuffix(src, dst, suffix string) error: Copy files with a specific suffix from the source directory to the destination directory, renaming the copied files.
  • CopyFile(src, dst string) error: Copy a single file from the source path to the destination path.
  • ParentDir(p string) string: Return the parent directory of the given path.
  • BaseDir(p string) string: Return the base directory of the given path.
  • ParentBaseDir(p string) string: Return the parent base directory of the given path.
  • CompressDir(dir string) error: Compress the specified directory into a zip file.
Hashing and Encryption
  • GetSha256Hash(str string) string: Calculate the SHA-256 hash value of the given string and return it.
Web
  • GetPage(c *gin.Context, DefaultPageSize int): Calculate the offset based on the page number parameter from the request and return it.
  • ParseToken(token, secret string): Parse and validate the given token, and return the claims contained in it along with any potential errors.
MongoDB Specific
  • MustMongoSlice2StringSlice(i interface{}) []string: Convert a MongoDB slice type to a Go string slice type, ignoring errors.
  • MongoSlice2StringSlice(i interface{}) ([]string, error): Convert a MongoDB slice type to a Go string slice type, returning errors.

Usage Example

package main

import (
    "fmt"
    "github.com/limitcool/lib"
)

func main() {
    slice := []int{1, 2, 3, 4, 3}
    uniqueSlice := lib.Unique(slice)
    fmt.Println(uniqueSlice) // Output: [1 2 3 4]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaseDir

func BaseDir(p string) string

BaseDir returns the base directory of the provided path

func CompressDir

func CompressDir(dir string) error

CompressDir compresses the directory into a zip file

func ConvertAnyToInterface

func ConvertAnyToInterface(anyValue *any.Any) (interface{}, error)

func ConvertInterfaceToAny

func ConvertInterfaceToAny(v interface{}) (*any.Any, error)

func CopyDir

func CopyDir(src, dst, skip string) error

CopyDir copies the directory from the source to the destination skip the file if you don't want to copy

func CopyDirHasSuffix

func CopyDirHasSuffix(src, dst, suffix string) error

CopyDirHasSuffix copies the directory from the source to the destination contain is the file if you want to copy, and rename copied filename with dir/index_filename

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies the file from the source to the destination

func Exclude

func Exclude[T comparable](slice []T, element T) []T

func FileExists

func FileExists(filename string) bool

FileExists checks if the file exists in the provided path

func FilesInFolder

func FilesInFolder(dir, filename string) ([]string, error)

FilesInFolder returns the filepath contains in the provided folder

func FolderExists

func FolderExists(foldername string) bool

FolderExists checks if the folder exists

func GetPage

func GetPage(c *gin.Context, DefaultPageSize int) int

func GetSha256Hash

func GetSha256Hash(str string) string

func GetStartOfDay

func GetStartOfDay() time.Time

func InSlice

func InSlice[T comparable](element T, Slice []T) bool

func IntToBool

func IntToBool[T constraints.Signed](a T) bool

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns a slice of the keys of the map. based with go 1.18 generics

func MongoSlice2StringSlice

func MongoSlice2StringSlice(i interface{}) ([]string, error)

func MustConvertAnyToInterface

func MustConvertAnyToInterface(anyValue *any.Any) interface{}

func MustConvertInterfaceToAny

func MustConvertInterfaceToAny(v interface{}) *any.Any

func MustMongoSlice2StringSlice

func MustMongoSlice2StringSlice(i interface{}) []string

func ParentBaseDir

func ParentBaseDir(p string) string

ParentBaseDir returns the parent base directory of the provided path

func ParentDir

func ParentDir(p string) string

ParentDir returns the parent directory of the provided path

func ParseToken

func ParseToken(token, secret string) (*jwt.MapClaims, error)

ParseToken 解析和校验token

func Pop

func Pop[T any](slice []T) []T

func PopFront

func PopFront[T any](slice []T) []T

func Ptr

func Ptr[T any](t T) *T

func Push

func Push[T any](slice []T, element T) []T

func ReadFile

func ReadFile(filename string) (string, error)

ReadFile reads the file from the provided path

func Reverse

func Reverse[T any](s []T) []T

func SetDebugMode

func SetDebugMode(debugFunction func())

func ToInterfaceSlice

func ToInterfaceSlice[T any](slice []T) []interface{}

func Unique

func Unique[T comparable](slice []T) []T

func Zero

func Zero[T any]() T

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL