modulepackage
0.0.0-20240810041803-eb9e4eaa70ea
Repository: https://github.com/limitcool/lib.git
Documentation: pkg.go.dev
# README
lib
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 protobufany.Any
type to Go'sinterface{}
type.ConvertInterfaceToAny(v interface{}) (*any.Any, error)
: Convert Go'sinterface{}
type to Google's protobufany.Any
type.MustConvertInterfaceToAny(v interface{}) *any.Any
: Similar toConvertInterfaceToAny
, but ignores errors.MustConvertAnyToInterface(anyValue *any.Any) interface{}
: Similar toConvertAnyToInterface
, 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]
}
# Functions
BaseDir returns the base directory of the provided path.
CompressDir compresses the directory into a zip file.
No description provided by the author
No description provided by the author
CopyDir copies the directory from the source to the destination skip the file if you don't want to copy.
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.
CopyFile copies the file from the source to the destination.
No description provided by the author
FileExists checks if the file exists in the provided path.
FilesInFolder returns the filepath contains in the provided folder.
FolderExists checks if the folder exists.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Keys returns a slice of the keys of the map.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ParentBaseDir returns the parent base directory of the provided path.
ParentDir returns the parent directory of the provided path.
ParseToken 解析和校验token.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ReadFile reads the file from the provided path.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author