Categorygithub.com/adamnasrudin03/go-helpers
modulepackage
0.0.9
Repository: https://github.com/adamnasrudin03/go-helpers.git
Documentation: pkg.go.dev

# README

go-helpers

This is a helper library for the GoLang project.

Features

String helpers

FunctionsDescription
ToLowerConverts a given string to lower case.
ToUpperConverts a given string to upper case.
ToSentenceCaseConverts a given string to sentence case.
ToTitleConverts a given string to title case or Capitalized Each Word.
GenerateRandomStringGenerates a random string of a specified length using the alphabet characters. It uses the current time as a seed for the random number generator.
GeneratePasswordGenerates a random password of a specified length using a combination of lowercase letters, uppercase letters, numbers, and special characters.
GenerateUUIDGenerates a new V7 (random) UUID. The function returns the generated UUID and an error if any.
HashPasswordGenerates a hashed password from a plain text password using bcrypt. The function returns the hashed password and an error if any.
PasswordIsValidChecks if a given plain text password matches the hashed password. The function returns true if the passwords match, false otherwise.
CheckStringValueChecks if a given string is not empty. The function returns true if the string is not empty, false otherwise.
CheckStringValueToPointerChecks if a given string is not empty and returns a pointer to the string. The function returns a pointer to the string if the string is not empty, nil otherwise.
TranslateTranslates a given string from source language to target language. The function returns the translated string and an error if any.
IsUUIDChecks if the given string is a valid UUID. The function returns true if the string is a valid UUID, false otherwise.
IsEmailChecks if the given string is a valid email. The function returns true if the string is a valid email, false otherwise.
IsNumberChecks if the given string is a valid number. The function returns true if the string is a valid number, false otherwise.
IsPhoneNumberIdChecks if the given string is a valid phone number ID. The function returns true if the string is a valid phone number ID, false otherwise.

Number helpers

FunctionsDescription
RoundUpFloatRounds up the given float64 to the given uint precision. For example, if the input is 12.345 and the precision is 2, this function will return 12.35.
RoundDownFloatRounds down the given float64 to the given uint precision. For example, if the input is 12.345 and the precision is 2, this function will return 12.34.
RoundFloatRounds the given float64 to the given uint precision, based on the rounding mode. The rounding mode is determined by the roundingUp parameter.
GenerateRandomNumberGenerates a random number within a specified length. The length parameter determines the maximum value of the generated number.
GetMinMaxIntArrayGet the minimum and maximum values in an array of int. The function returns the minimum and maximum values as a tuple.
CheckArrayFloat64NilChecks if the input array of float64 is nil or empty. If not, it returns the input array. If the input array is nil or empty, it returns an empty array.
CheckFloat64ValueChecks if the input pointer to a float64 is nil or empty. If not, it returns the value of the input pointer. If the input pointer is nil, it returns 0.0.

Json helpers

FunctionsDescription
SafeJsonMarshalMarshals a given data to json. If the Marshal process is failed, it will return the original Marshal result and the error.
JsonToStructUnMarshals a json string to a struct. If the UnMarshal process is failed, it will return the original Marshal result and the error.
JsonToStringMarshals a struct to json string. If the Marshal process is failed, it will return the original Marshal result and the error.

Net helpers

FunctionsDescription
QueryEscapeEscape a string for use in a URL query.
StreamToStringReads the entire contents of an io.Reader and returns it as a string.
StreamToByteReads the entire contents of an io.Reader and returns it as a byte slice.
GetHTTPRequestJSONSends an HTTP request with the given method, URL, body, and timeout, and returns the response as a byte slice. The function takes an optional set of headers to include with the request.
GetHTTPRequestSkipVerifySends an HTTP request with the given method, URL, body, and timeout, and returns the response as a byte slice. The function takes an optional set of headers to include with the request.
NewHttpDecoderCreates a new HttpDecoder, which can be used to decode HTTP requests.

Time helpers

Response mapper

Others

FunctionsDescription
FormatErrorValidatorFormats multiple validation error messages. It takes a slice of validator.ValidationErrors and returns a slice of strings, where each string is a formatted error message.
FormatErrorValidatorSingleFormats a single validation error message. It takes a validator.ValidationErrors and returns a formatted error message.
PanicRecoverThis function is used to recover from a panic. It takes a string as an argument and prints it to the console.

Installation

  go get github.com/adamnasrudin03/go-helpers@latest

Usage

Check in go playground: https://go.dev/play/p/Qidzj-zwSa1

package main

import (
	"fmt"

	help "github.com/adamnasrudin03/go-helpers"
)

func main() {
	fmt.Println(help.ToLower("Lorem ipsum dolor sit amet."))        // output; lorem ipsum dolor sit amet.
	fmt.Println(help.ToUpper("Lorem ipsum dolor sit amet."))        // output; LOREM IPSUM DOLOR SIT AMET.
	fmt.Println(help.ToSentenceCase("LOREM IPSUM DOLOR SIT AMET.")) // output; Lorem ipsum dolor sit amet.
	fmt.Println(help.ToTitle("Lorem ipsum dolor sit amet."))        // output; Lorem Ipsum Dolor Sit Amet.
}

other examples: https://github.com/adamnasrudin03/go-helpers/tree/main/examples

How to Contribute

Make a pull request...

License

Distributed under MIT License, please see license file within the code for more details.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

CheckArrayFloat64Nil checks if the input array of float64 is nil or empty.
CheckArrayIntNil checks if the input array of integers is nil or empty.
CheckFloat64Value checks if the input pointer to a float64 is nil or empty.
CheckIntValue checks if the input pointer of integer is nil or empty.
CheckStringValue returns an empty string if the input pointer to a string is nil, otherwise it returns the value of the input pointer.
CheckStringValueToPointer returns a pointer to a string or nil if the input string is empty or only contains whitespace.
CheckTimeIsZeroToPointer returns a pointer to a time.Time if the input time is not zero, otherwise it returns nil.
CheckTimeIsZeroToString converts a time.Time object to a string if it is not zero, otherwise it returns an empty string.
CheckTimePointerToString converts a pointer to a time.Time object to a string, using the specified format.
CheckTimePointerValue returns the value of a time.Time pointer.
GeneratePassword generates a random password of a specified length using a combination of lowercase letters, uppercase letters, numbers, and special characters.
GenerateRandomNumber generates a random number within a specified length.
GenerateRandomString generates a random string of a specified length using the alphabet characters.
GenerateUUID generates a new UUID v7 It returns a UUID v7 and an error, if any.
GetHTTPRequestJSON sends an HTTP request with the given method, URL, body, and timeout, and returns the response as a byte slice.
GetHTTPRequestSkipVerify sends an HTTP request with the given method, URL, body, and timeout, and returns the response as a byte slice.
GetMinMaxIntArray calculates the minimum and maximum values in an array of Int.
HashPassword generates a hashed password from a plain text password using bcrypt.
IsEmail checks if the given email is in a valid format.
IsNumber checks if the given string is a valid number.
IsPhoneNumberId checks if the given string is a valid Indonesian phone number.
IsUUID checks if the given string is a valid UUID.
JsonToString is a function that marshal a struct to json string.
JsonToStruct is a function that unmarshal a json string to a struct.
NewHttpDecoder creates a new HttpDecoder with default settings.
NewTimeUTC7 creates and returns a new TimeUTC7 instance.
No description provided by the author
PasswordIsValid checks if a given plain text password matches the hashed password.
QueryEscape is a wrapper around the net/url.QueryEscape function.
RoundDownFloat rounds down the given float64 to the given uint precision.
check https://adamnasrudin.vercel.app/cheat-sheet/rounding-float-using-golang.
RoundUpFloat rounds up the given float64 to the given uint precision.
SafeJsonMarshal is a function that safely marshal a data to json.
StreamToByte converts an io.Reader to a byte slice.
StreamToString converts an io.Reader to a string.
ToLower converts a given string to lower case.
ToSentenceCase converts a given string to sentence case.
ToTitle converts a given string to title case.
ToUpper converts a given string to upper case.
Translate text from source language to target language https://cloud.google.com/translate/automl/docs/reference/rest/v3/projects.locations/translateText.

# Constants

No description provided by the author
Timezone country.
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
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
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Format date time.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
indonesia country timezone in table city.
No description provided by the author
No description provided by the author
Month.
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
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
Month English(en) to Indonesian(id).
Mapping Timezone ToT imeLocation ...

# Structs

OptionDecoder is used to configure the decoder for custom types.
TimeUTC7 represents a time with its location set to UTC+7, specifically for Western Indonesia Time.

# Interfaces

HttpDecoder is the interface that decodes HTTP requests.

# Type aliases

OptionTimeUTC+7 is a function type used for applying options to TimeUTC7 instances.