Documentation
¶
Index ¶
- func CheckHttp(link string, timeout time.Duration) (*http.Response, error)
- func CheckIdCard(idCardStr string) bool
- func CheckPort(ip, port string, timeout time.Duration) error
- func ConvertDownloadCount(downloadCount uint64) string
- func DownloadFile(request *crawler.Request, savePath string, checkContentLength bool) (int64, error)
- func FormatByte(data, dividend float64) string
- func FormatEP(data float64) string
- func FormatGB(data, dividend float64) string
- func FormatKB(data, dividend float64) string
- func FormatMB(data, dividend float64) string
- func FormatPB(data, dividend float64) string
- func FormatTB(data, dividend float64) string
- func GenerateShortUrl(shortLinkPrefix string, link string) (string, error)
- func GetSSLExpireDate(domain string) (*time.Time, error)
- func IP2Long(ipAddress string) *big.Int
- func InArray(needle interface{}, haystack interface{}) bool
- func PaginateData(list interface{}, total int64, page, pageSize int) map[string]interface{}
- func ParseTime(t string) (*time.Time, error)
- func PathExists(path string) bool
- func Response(ctx *gin.Context, data interface{}, code int, message string)
- func SaveFile(reader io.Reader, savePath string) (int64, error)
- func ShuffleArray[T any](arr []T)
- func Ternary(condition bool, trueVal, falseVal interface{}) interface{}
- type GetIntervalTimeRequest
- type GetIntervalTimeResponse
- type IntervalTimeType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckHttp ¶ added in v0.0.27
CheckHttp is a function that checks if a given URL is accessible via HTTP. It uses the http.Head function from the net/http package to send a HEAD request to the URL. A HEAD request is similar to a GET request, but it only requests the headers and not the body of the response. This makes the function efficient for checking if a URL is accessible without downloading the entire content.
If the HEAD request is successful, the function returns nil, indicating that the URL is accessible. If the HEAD request is not successful, the function returns the error returned by http.Head, indicating that the URL is not accessible or that there was a problem in sending the request.
Parameters: link: a string representing the URL to check.
Returns: An error if the HEAD request could not be sent or if the URL is not accessible; otherwise, nil.
func CheckIdCard ¶
CheckIdCard is a function that checks if a given string is a valid Chinese ID card number. It supports both 15-digit and 18-digit ID card numbers, and the last digit of 18-digit ID card numbers can be 'X' or 'x'.
The function uses regular expressions to match the input string with the pattern of a valid ID card number. If the input string does not match the pattern, the function returns false.
If the input string matches the pattern, the function then checks the last digit of the ID card number. For 18-digit ID card numbers, the last digit is a check digit that is calculated based on the first 17 digits. The function calculates the check digit and compares it with the actual last digit of the input string. If they match, the function returns true; otherwise, it returns false.
Parameters: idCardStr: a string representing a Chinese ID card number.
Returns: A boolean value indicating whether the input string is a valid Chinese ID card number.
func CheckPort ¶ added in v0.0.27
CheckPort is a function that checks if a given port on a given IP address is open. It uses the net.DialTimeout function from the net package to attempt to establish a connection to the specified IP address and port within the specified timeout duration. If the connection is successful, the function closes the connection and returns nil, indicating that the port is open. If the connection is not successful, the function returns the error returned by net.DialTimeout, indicating that the port is not open or that there was a problem in establishing the connection.
Parameters: ip: a string representing the IP address to check. port: a string representing the port to check. timeout: a time.Duration representing the maximum amount of time to wait for the connection to be established.
Returns: An error if the connection could not be established within the specified timeout duration or if there was a problem in establishing the connection; otherwise, nil.
func ConvertDownloadCount ¶ added in v0.0.11
ConvertDownloadCount is a function that converts a download count to a string representation. The function supports download counts up to 100 million and above.
The function checks if the download count is greater than or equal to 100 million. If it is, the function converts the download count to a float, divides it by 100 million, and formats it as a string with two decimal places followed by "亿次下载".
If the download count is less than 100 million but greater than or equal to 10,000, the function divides it by 10,000 and formats it as a string followed by "万次下载".
If the download count is less than 10,000, the function formats it as a string followed by "次下载".
Parameters: downloadCount: a uint64 representing the download count.
Returns: A string representing the download count in a more readable format.
func DownloadFile ¶ added in v0.0.21
func DownloadFile(request *crawler.Request, savePath string, checkContentLength bool) (int64, error)
DownloadFile is a function that downloads a file from a given URL and saves it to a specified path. It uses the http package to send a GET request to the URL and receive the response. If there is an error in sending the request or receiving the response, the function returns the error.
The function then gets the directory path of the save path. If the directory does not exist, the function creates it. If there is an error in creating the directory, the function returns the error.
The function then creates a new file at the save path. If there is an error in creating the file, the function returns the error.
The function then writes the body of the response to the file. If there is an error in writing to the file, the function returns the error.
Parameters: request: a pointer to a crawler.Request representing the request to be sent. savePath: a string representing the path where the file is to be saved. checkContentLength: a boolean indicating whether to check if the downloaded file size matches the content length.
Returns: The size of the downloaded file and an error if there was a problem in downloading or saving the file.
func FormatByte ¶ added in v0.0.32
FormatByte converts a byte value to a human-readable string representation in bytes (B) or kilobytes (KB).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to kilobytes.
Returns: - A string representing the formatted byte value.
func FormatEP ¶ added in v0.0.32
FormatEP converts a byte value to a human-readable string representation in exabytes (EP).
Parameters: - data: The byte value to be formatted.
Returns: - A string representing the formatted exabyte value.
func FormatGB ¶ added in v0.0.32
FormatGB converts a byte value to a human-readable string representation in gigabytes (GB) or terabytes (TB).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to terabytes.
Returns: - A string representing the formatted gigabyte value.
func FormatKB ¶ added in v0.0.32
FormatKB converts a byte value to a human-readable string representation in kilobytes (KB) or megabytes (MB).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to megabytes.
Returns: - A string representing the formatted kilobyte value.
func FormatMB ¶ added in v0.0.32
FormatMB converts a byte value to a human-readable string representation in megabytes (MB) or gigabytes (GB).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to gigabytes.
Returns: - A string representing the formatted megabyte value.
func FormatPB ¶ added in v0.0.32
FormatPB converts a byte value to a human-readable string representation in petabytes (PB) or exabytes (EP).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to exabytes.
Returns: - A string representing the formatted petabyte value.
func FormatTB ¶ added in v0.0.32
FormatTB converts a byte value to a human-readable string representation in terabytes (TB) or petabytes (PB).
Parameters: - data: The byte value to be formatted. - dividend: The threshold value to determine if the byte value should be converted to petabytes.
Returns: - A string representing the formatted terabyte value.
func GenerateShortUrl ¶ added in v0.0.28
GenerateShortUrl GenerateShortUrlKey is a function that generates a short URL key from a given URL.
func IP2Long ¶ added in v0.0.16
IP2Long is a function that converts an IP address to a big integer. The function supports both IPv4 and IPv6 addresses.
The function uses the net package to parse the input string into an IP address. If the input string is not a valid IP address, the function returns nil.
The function then checks if the IP address is an IPv6 address by looking for a colon in the input string. If the IP address is an IPv6 address, the function converts it to a 16-byte representation and sets it to a big integer. If the IP address is an IPv4 address, the function converts it to a 4-byte representation and sets it to a big integer.
Parameters: ipAddress: a string representing an IP address.
Returns: A pointer to a big integer representing the IP address. If the input string is not a valid IP address, the function returns nil.
func InArray ¶ added in v0.0.25
func InArray(needle interface{}, haystack interface{}) bool
InArray is a function that checks if a given element (needle) is present in a given collection (haystack). The function supports collections of type slice, array, and map.
The function uses the reflect package to get the value of the haystack and its kind. If the kind of the haystack is either a slice or an array, the function iterates over the elements of the haystack. For each element, it checks if the element is deeply equal to the needle. If it finds a match, it returns true.
If the kind of the haystack is a map, the function iterates over the keys of the map. For each key, it checks if the value associated with the key is deeply equal to the needle. If it finds a match, it returns true.
If the kind of the haystack is neither a slice, an array, nor a map, the function panics with a message.
If the function does not find a match after checking all elements or values, it returns false.
Parameters: needle: an element to be searched in the haystack. haystack: a collection where the needle is to be searched.
Returns: A boolean value indicating whether the needle is present in the haystack.
func PaginateData ¶ added in v0.0.29
func ParseTime ¶ added in v0.0.16
ParseTime is a function that parses a string into a time.Time object. It takes a string representing a time in the format "2006-01-02 15:04:05" as a parameter, and returns a pointer to a time.Time object and an error. If the string cannot be parsed into a time.Time object, the function returns nil and the error.
func PathExists ¶ added in v0.0.9
PathExists is a function that checks if a given path exists in the file system.
The function uses the os package to get the file or directory information of the given path. If there is an error in getting the information, the function checks if the error is because the file or directory exists. If the file or directory exists, the function returns true; otherwise, it returns false.
If there is no error in getting the information, the function returns true, indicating that the path exists.
Parameters: path: a string representing the path to be checked.
Returns: A boolean value indicating whether the given path exists in the file system.
func Response ¶ added in v0.0.9
Response is a function that sends a JSON response to the client.
The function uses the gin package to send a JSON response with a given HTTP status code, message, and data. The response is a JSON object with two properties: "message" and "data". The "message" property is a string that represents the message to be sent to the client. The "data" property is an interface{} that represents the data to be sent to the client.
Parameters: ctx: a pointer to a gin.Context that represents the context of the request. data: an interface{} that represents the data to be sent to the client. code: an int that represents the HTTP status code of the response. message: a string that represents the message to be sent to the client.
Returns: The function does not return a value.
func SaveFile ¶ added in v0.0.31
SaveFile is a function that saves the content read from an io.Reader to a specified file path. The function first checks if the directory of the save path exists, if not, it creates it. Then, it creates a new file at the save path. After that, it writes the content read from the io.Reader to the file. If there is an error at any step, the function returns the error and the number of bytes written so far.
Parameters: reader: an io.Reader from which the content is read. savePath: a string representing the path where the content is to be saved.
Returns: The number of bytes written to the file and an error if there was a problem in creating the directory, creating the file, or writing to the file.
func ShuffleArray ¶ added in v0.0.31
func ShuffleArray[T any](arr []T)
ShuffleArray is a function that shuffles the elements of a given array. The function uses the math/rand package to generate a new random source based on the current time. It then uses this random source to shuffle the elements of the array.
The function uses a type parameter T, which means it can be used with arrays of any type. The function takes an array of type T as a parameter and returns an array of type T.
The function uses a closure in the call to the Shuffle method. This closure swaps the elements at the provided indices.
Parameters: arr: an array of type T representing the array to be shuffled.
Returns: An array of type T representing the shuffled array.
func Ternary ¶ added in v0.0.32
func Ternary(condition bool, trueVal, falseVal interface{}) interface{}
Ternary is a function that implements the ternary operator in Go. It takes a boolean condition and two values as input. If the condition is true, it returns the first value; otherwise, it returns the second value.
Types ¶
type GetIntervalTimeRequest ¶ added in v0.0.16
type GetIntervalTimeRequest struct { // Time is the time that specifies the date. Time time.Time // Type is the type of the time interval. Type IntervalTimeType // Num is the number that specifies the offset from the specified date. Num int }
GetIntervalTimeRequest is a struct that represents a request to get the start and end times of a time interval. It contains the time that specifies the date, the type of the time interval, and the number that specifies the offset from the specified date.
type GetIntervalTimeResponse ¶ added in v0.0.16
type GetIntervalTimeResponse struct { // StartAt is the start time of the time interval. StartAt time.Time // EndAt is the end time of the time interval. EndAt time.Time }
GetIntervalTimeResponse is a struct that represents the response to a GetIntervalTimeRequest. It contains the start and end times of the time interval.
func GetIntervalTime ¶ added in v0.0.16
func GetIntervalTime(req *GetIntervalTimeRequest) (resp *GetIntervalTimeResponse)
GetIntervalTime is a function that gets the start and end times of a time interval. It takes a pointer to a GetIntervalTimeRequest struct as a parameter, and returns a pointer to a GetIntervalTimeResponse struct. The function calculates the start and end times based on the type and number in the GetIntervalTimeRequest, and sets them in the GetIntervalTimeResponse.
type IntervalTimeType ¶ added in v0.0.16
type IntervalTimeType int
IntervalTimeType is a type that represents the type of a time interval.
const ( Day IntervalTimeType = iota Week Month Year )
Constants for the different types of time intervals.