Categorygithub.com/indivizo/go-utils
modulepackage
0.0.0-20190725082737-3f0f9c272df3
Repository: https://github.com/indivizo/go-utils.git
Documentation: pkg.go.dev

# README

Go utils

Helper package for our go projects.

Slice

StringInSlice()

JSON

RenderDataAsJSON()

WriteJson()

Request

ParseFromRequest()

ReadAndRewind()

GetMatchingPrefixLength()

SendRequest()

This function will try to send the request in the background several times if the response is failes.

Example usage:

type MyType struc {
  Field1 string `json:"field1"`
  Field2 string `json:"field2"`
}
myValue := MyType{
  Field1: "Hello",
  Field2: "GO",
}
queryBody, _ := json.Marshal(myValue)
contentReader := bytes.NewBuffer(queryBody)
  go func() {
    cancel := make(chan bool)
    res := utils.SendRequest(utils.Request{
      URL:     "http://example.com",
      Method:  "POST",
      Body:    contentReader,
      Headers: []http.Header{{"Content-Type": []string{"application/json"}}},
    })

    response := <-res
    if response == nil {
      log.Println("request failed")
    } else {
      log.Printf("cool, the reponse is: %v\n", response)
    }
  }()

If you want to cancel the request just set it in the cancel chanel:

go func() {
    cancel := make(chan bool)
    res := utils.SendRequest(utils.Request{
      URL:     "http://example.com",
      Method:  "POST",
      Body:    contentReader,
      Headers: []http.Header{{"Content-Type": []string{"application/json"}}},
      Cancel:  cancel,
    })
    time.Sleep(time.Second * 5)
    cancel <- true
  }()

Or if you want to combine them:

go func() {
    cancel := make(chan bool)
    res := utils.SendRequest(utils.Request{
      URL:     "http://example.com",
      Method:  "POST",
      Body:    contentReader,
      Headers: []http.Header{{"Content-Type": []string{"application/json"}}},
      Cancel:  cancel,
    })
    go func() {
      time.Sleep(time.Second * 5)
      cancel <- true
    }()
    response := <-res
    if response == nil {
      log.Println("this request is failed")
    } else {
      log.Printf("cool, the reponse is: %v\n", response)
    }
  }()

Misc

RandomHash()

Error

ErrInvalidUrl

ErrInvalidMail

ErrInvalidMongoIdHash

Types

Url

  • String()
  • Validate()

Email

  • String()
  • Validate()

# Packages

No description provided by the author

# Functions

GetMatchingPrefixLength returns the length of the path a pattern could match as a prefix.
We can't use jwt.ParseFromRequest() because it calls ParseMultipartForm() and it will break MultipartReader() which is important for file upload handling.
No description provided by the author
ReadAndRewind Reads an io.ReadCloser into an io.Reader and replaces the original source with the buffered source.
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
Writes the given data as a json and set the status code.

# Constants

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
No description provided by the author

# Structs

Request is a structure to store the details of a network request.

# Type aliases

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