helper

package module
v0.0.0-...-7e9beb7 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2020 License: GPL-3.0 Imports: 29 Imported by: 0

README

Go Helper

Go Helper is a Golang library for various needs.

Installation

Assumption Go is already installed and configured just use go get command.

go get github.com/ihsankurniawan/go-helper

Usage

Array
import "github.com/ihsankurniawan/go-helper"

// Can be used for basic types such as int, uint, string, etc
var a int
var b []int
a = 1
b = []int{1, 2, 3}

rs, idx := helper.InArray(a, b) // returns true, 0

// Same like InArray function but not returning index
rs = helper.InArrayNoIndex(a, b) // returns true

// Same like InArray function but only for string, faster than InArray
rs = helper.InArrayContains("foo", []string{"foo", "bar"}) // returns true
File
import "github.com/ihsankurniawan/go-helper"

// Function to save multipart file into desired location
var someFile multipart.File

err := helper.SaveFileToDisk(someFile, "/tmp/someFile.jpg") // returns error if there any
Image
import "github.com/ihsankurniawan/go-helper"

// Convert base64 of image to standard img file
// Currently only support jpeg and png
// returns image and error if there any
img, err := helper.Base64ToImg("/tmp/temporary-file", "base64encoded of image file") 
String
import "github.com/ihsankurniawan/go-helper"

// Randomize string with given length and charset
// You can also use constant in this helper, available:
// STRING_NUMBER        = "0123456789"
// STRING_ALPHABET_LOWER= "abcdefghijklmnopqrstuvwxyz"
// STRING_ALPHABET_UPPER= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// STRING_ALPHANUMERIC 	= STRING_ALPHABET_LOWER + STRING_ALPHABET_UPPER + STRING_NUMBER
str := helper.StringRandomWithCharset(5, "abcde") // return "aabad"

// Randomize string with lowercase alphabet, uppercase alphabet, number with given length
str = helper.StringRandom(5) // return "ab12F"

// Function to check whether string can be converted to json or not
isJson := helper.StringIsJSON("{}") // return true

// Function to convert int to IDR currency format
str = helper.FormatToIdrCurrency(100000) // return "100.000"

// Remove whitespace from string
str = helper.WhiteSpaceRemove(" lorem ipsum ") // return "loremipsum"
Struct
import "github.com/ihsankurniawan/go-helper"

// Function to check whether given struct is empty or not
isEmpty := helper.IsEmpty(struct{}{}) // return true 
Telco Operator
This is only for Indonesian operator only
import "github.com/ihsankurniawan/go-helper"

// Function to check whether given phone number is from Telkomsel operator
isTelkomsel := helper.TselPrefix("6281312341234") // return true 

// Function to check whether given phone number is from Indosat operator
isIndosat := helper.IsatPrefix("6285612341234") // return true 

// Function to check whether given phone number is from XL operator
isIndosat := helper.XlPrefix("6281712341234") // return true 

// Function to check whether given phone number is from Three operator
isIndosat := helper.ThreePrefix("6289512341234") // return true 
Struct
import "github.com/ihsankurniawan/go-helper"

// Function to get Time in Asia/Jakarta GMT+7 location based
now := helper.GetJakartaNowTime() // return time.Time
URL
import "github.com/ihsankurniawan/go-helper"

// Helper to extract json data from given URL, only available for GET method
type SomeStruct struct{
    Id    int    `json:"id"`
    Data  string `json:"data"`
}
var str SomeStruct

// var str will be filled if json data match
err := helper.GetJsonDataFromUrl("https://someurl.com", &str) // return error if there any

// Basically same like above function, only difference is URL returns XML data
err = helper.GetXmlDataFromUrl("https://someurl.com", &str) // return error if there any

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

View Source
const (
	ONE_KILO      = 1000
	ONE_KILOBYTE  = 1024
	ONE_MILLION   = 1000000
	ONE_MEGABYTE  = 1048576
	OUNCE_TO_GRAM = 32.148
)
View Source
const (
	STRING_NUMBER         = "0123456789"
	STRING_ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz"
	STRING_ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	STRING_ALPHANUMERIC   = STRING_ALPHABET_LOWER + STRING_ALPHABET_UPPER + STRING_NUMBER
)
View Source
const (
	MYSQL_DATE_FORMAT     = "2006-01-02"
	MYSQL_TIME_FORMAT     = "15:04:05"
	MYSQL_DATETIME_FORMAT = MYSQL_DATE_FORMAT + " " + MYSQL_TIME_FORMAT
	ISO8601_TIME_FORMAT   = "2006-01-02T15:04:03.000-0700"
)

Variables

View Source
var JakartaTime = time.FixedZone("Asia/Jakarta", 7*60*60)

JakartaTime is default location of application with timezone = Asia/Jakarta (UTC+7)

Functions

func AesDecrypt

func AesDecrypt(cipherText string, secretKey string) (string, error)

func AesEncrypt

func AesEncrypt(plainText string, secretKey string) (string, error)

func Base64Encode

func Base64Encode(s string) string

func Base64ToImg

func Base64ToImg(tmp string, src string) (*os.File, error)

Convert base64 of image to standard img file Currently only support jpeg and png

func ComputeHmac256

func ComputeHmac256(message string, secret string) string

func FormatToIdrCurrency

func FormatToIdrCurrency(n int) string

Function to convert int to IDR currency format Ex: FormatToIdrCurrency(100000) // return "100.000"

func GetJakartaNowTime

func GetJakartaNowTime() time.Time

func GetJsonDataFromUrl

func GetJsonDataFromUrl(url string, target interface{}) error

Helper to extract json data from given URL, should be using GET method var str SomeStruct Ex: GetJson("https://someurl.com", &str)

func GetXmlDataFromUrl

func GetXmlDataFromUrl(url string, target interface{}) error

Helper to extract XML data from given URL, should be using GET method var str SomeStruct Ex: GetXml("https://someurl.com", &str)

func InArray

func InArray(val interface{}, array interface{}) (exists bool, index int)

Ex: var a int var b []int

a = 1 b = []int{1, 2, 3}

InArray(a, b) // return true, 0

Could be used to check other type as well, such as string, uint, other basic types

func InArrayContains

func InArrayContains(val string, checkArray []string) bool

Same like InArray function but only for string, faster than InArray

func InArrayNoIndex

func InArrayNoIndex(val interface{}, array interface{}) bool

Same like InArray function but not returning index Could be used to check other type as well, such as string, uint, other basic types

func IsEmpty

func IsEmpty(object interface{}) bool

func IsValidUrl

func IsValidUrl(uri string) bool

isValidUrl tests a string to determine if it is a well-structured url or not. https://golangcode.com/how-to-check-if-a-string-is-a-url/ Ex: IsValidUrl("https://someurl.com")

func IsatPrefix

func IsatPrefix(hp string) bool

func Md5Encrypt

func Md5Encrypt(text string) string

func SaveFileToDisk

func SaveFileToDisk(file multipart.File, saveLoc string) error

Function to save multipart file into desired location Ex: SaveFileToDisk(file, "/tmp/temporary_file")

func Sha1Encrypt

func Sha1Encrypt(s string) string

func Sha1_HMAC

func Sha1_HMAC(k string, message string) string

func Sha256Encrypt

func Sha256Encrypt(s string) string

func Sha256_HMAC

func Sha256_HMAC(k string, message string) string

func Sha512Encrypt

func Sha512Encrypt(s string) string

func StringIsJSON

func StringIsJSON(s string) bool

Function to check whether string can be converted to json or not

func StringRandom

func StringRandom(length int) string

Randomize string with lowercase alphabet, uppercase alphabet, number with given length Ex: StringRandom(5) // return "ab12F"

func StringRandomWithCharset

func StringRandomWithCharset(length int, charset string) string

Randomize string with given length and charset Ex: StringRandomWithCharset(5, "abcde") // return "aabad"

func ThreePrefix

func ThreePrefix(hp string) bool

func TselPrefix

func TselPrefix(hp string) bool

func WhiteSpaceRemove

func WhiteSpaceRemove(str string) string

Remove whitespace from string Ex: WhiteSpaceRemove(" lorem ipsum ") // return "loremipsum"

func XlPrefix

func XlPrefix(hp string) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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