Giter Club home page Giter Club logo

goutil's Introduction

Utilities for Go

Installation

go get -u github.com/vannleonheart/goutil

Random String

Generate Using Default Character Set

randomString := goutil.NewRandomString("")

// random string with exactly 100 characters in length
length := 100
exactRandomString := randomString.Generate(length)

// random string with random length between 10 and 25 characters
minLength := 10
maxLength := 25
rangedRandomString := randomString.GenerateRange(minLength, maxLength)

fmt.Println(exactRandomString)
fmt.Println(rangedRandomString)

Set Custom Character Set

newCharset := "abc123@#$"

randomString.SetCharset(newCharset)

// random string with exactly 10 characters in length
// using only character set abc123@#$
fmt.Println(randomString.Generate(10))

Use From Predefined Character Set

// random string with exactly 15 characters in length
// using hexadecimal character set
fmt.Println(randomString.WithCharset(goutil.HexadecimalCharset).Generate(15))

Available Predefined Character Set

Const Character Set
AlphaCharset abcdefghijklmnopqrstuvwxyz
AlphaUCharset ABCDEFGHIJKLMNOPQRSTUVWXYZ
NumCharset 0123456789
AlphaNumCharset AlphaCharset + NumCharset
AlphaUNumCharset AlphaUCharset + NumCharset
AlphaAllNumCharset AlphaCharset + AlphaUNumCharset
HexadecimalCharset NumCharset + "abcdef"
SymbolCharset ~!@#$%^&*()_-+=[{}]|;:,<.>?

Config

Load From JSON File

jsonFilePath := "{your_json_file_path}"

var output map[string]interface{}

ptrByteFileContent, err := goutil.LoadJsonFile(jsonFilePath, &output)

if err != nil {
    // handle error
}

// convert pointer of []byte to string
// will return json file content in string
stringFileContent := string(*ptrByteFileContent)

fmt.Println(stringFileContent)


// will unmarshall json string to desired data type
// in this case to map[string]interface{}
fmt.Println(output)

HTTP Request

Generate Query String

toQueryString := map[string]interface{}{
	"page": 1,
	"search": "keyword",
}

queryString, err := goutil.GenerateQueryString(toQueryString)

if err != nil {
    // handle error
}

// will print page=1&search=keyword
fmt.Println(*queryString)

Sending Http Request

var result map[string]interface{}

targetUrl := "{your_target_url}"

requestData := map[string]interface{}{
	"page": 1,
}

requestHeaders := map[string]string{
	"Content-Type": "application/json",
}

ptrByteResponseBody, err := goutil.SendHttpRequest(http.MethodGet, targetUrl, &requestData, &requestHeaders, &result)

if err != nil {
    // handle error
}

// convert pointer of []byte to string
// will return response body in string
stringResponseBody := string(*ptrByteResponseBody)

fmt.Println(stringResponseBody)

// will unmarshall json string to desired data type
// in this case to map[string]interface{}
fmt.Println(result)

Sending HTTP Get Request

ptrByteResponseBody, err := goutil.SendHttpGet(targetUrl, &requestData, &requestHeaders, &result)

Sending HTTP Post Request

ptrByteResponseBody, err := goutil.SendHttpPost(targetUrl, &requestData, &requestHeaders, &result)

Sending HTTP Put Request

ptrByteResponseBody, err := goutil.SendHttpPut(targetUrl, &requestData, &requestHeaders, &result)

Sending HTTP Patch Request

ptrByteResponseBody, err := goutil.SendHttpPatch(targetUrl, &requestData, &requestHeaders, &result)

Sending HTTP Delete Request

ptrByteResponseBody, err := goutil.SendHttpDelete(targetUrl, &requestHeaders, &result)

Writing To File

Write String To File
A new file will be created if the file does not exist. If the file already exists, the string will be appended to the file as new line.

fileContent := "{your_file_content}"
filePath := "{your_file_path}"
fileName := "{your_file_name}"
extension := "{your_file_extension}"
rotation:= goutil.Daily

err := goutil.WriteStringToFile(fileContent, filePath, fileName, extension, rotation)

if err != nil {
    // handle error
}

Write JSON data to file
A new file will be created if the file does not exist. If the file already exists, the string will be appended to the file as new line.

fileContent := map[string]interface{}{
    "key1": "value1",
    "key2": "value2",
}
filePath := "{your_file_path}"
fileName := "{your_file_name}"
extension := "{your_file_extension}"
rotation:= goutil.Monthly

err := goutil.WriteJsonToFile(fileContent, filePath, fileName, extension, rotation)

if err != nil {
    // handle error
}

Constants for File Rotation

const Hourly = "hourly"         // will append suffix with format yyyy-MM-dd-HH to filename
const Daily = "daily"           // will append suffix with format yyyy-MM-dd to filename
const Weekly = "weekly"         // will append suffix with format yyyy-MM-WW to filename
const Monthly = "monthly"       // will append suffix with format yyyy-MM to filename
const Yearly = "yearly"         // will append suffix with format yyyy to filename

goutil's People

Contributors

vannleonheart avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.