utils

package module
v0.0.0-...-f966b7f Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 5 Imported by: 6

README

golang-utils

Util methods used in my different Golang projects

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateStringDistance

func CalculateStringDistance(baseDistance int, name string) string

CalculateStringDistance calculates the number of empty characters required to match a longer string above it (i.e. in a printed table)

Receives:

  • baseDistance (int) - Length of the string to match
  • name (string) - String to add empty characters to

Returns:

  • string - New string with the required added spaces to match the string above it

func FindStringInSlice

func FindStringInSlice(str string, sl []string) bool

FindStringInSlice detects if a string is in a slice.

Receives:

  • str (string) - String that will be searched inside the slice
  • sl ([]string) - Slice of strings

Returns:

  • bool - Whether or not the string was found inside the slice

func GetCurrentDateTime

func GetCurrentDateTime() string

GetCurrentDateTime returns the current date/time

  • Returns: string - Current date/time in YYYY-mm-dd HH:ii::ss format

func HandleError

func HandleError(err error)

HandleError is a basic error handling method. Prints out the error that occurred and exits the application.

func IsEmpty

func IsEmpty(field string) bool

IsEmpty checks if a required string field is empty

func LogMessage

func LogMessage(message string)

LogMessage simply logs a message to the console with the current date/time.

func SetResponse

func SetResponse(w http.ResponseWriter, statusCode int, body HTTPResponse)

SetResponse sets the response to be sent to the user in any API endpoints.

Receives:

  • w (http.ResponseWriter) - Go's HTTP ResponseWriter struct
  • statusCode (int) - HTTP status code of the response
  • body (HTTPResponse) - Body of the HTTP response using a custom struct

Types

type HTTPResponse

type HTTPResponse struct {
	Success bool        `json:"success"` // Whether the request was successful or not
	Rows    int         `json:"rows"`    // Number of rows in the Data interface response
	Data    interface{} `json:"data"`    // Interface that has the relevant information to return. Can be a slice, object, string, etc.
	Error   string      `json:"error"`   // Error message (in case an error occurred)
}

HTTPResponse is the struct used to send back JSON responses in the API

Jump to

Keyboard shortcuts

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