swissknife

package module
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 24 Imported by: 3

README

swiss-knife

Library with all sorts of things to create code prototypes super fast

install

go get github.com/Sagleft/swiss-knife
usage example
package main

import (
    "fmt"
    swissknife "github.com/Sagleft/swiss-knife"
)

func main() {
    fmt.Println("Hello, World!")
    swissknife.RunInBackground()
}

You might also be interested in:

automate crypto trading


image

🌐 Telegram channel

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckErrors added in v1.0.1

func CheckErrors(errChecks ...ErrorFunc) error

CheckErrors - check some errors

func FloatToString added in v1.0.3

func FloatToString(val float64) string

FloatToString - convert float to string

func FormatDuration added in v1.0.4

func FormatDuration(duration time.Duration) string

FormatDuration - format duration 52h -> "2 days 4 hours"

func GetRandomInt added in v1.0.8

func GetRandomInt(max int) int

GetRandomInt returns non-negative pseudo-random number in the half-open interval [0,max)

func GetRandomString added in v1.0.7

func GetRandomString(length int) string

GetRandomString - superfast string generation. 139 ns/op 32 B/op 2 allocs/op. topic: https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

func GetValuePrecision

func GetValuePrecision(val float64) int

GetValuePrecision - find out the precision of the numeric value

func HttpGET added in v1.5.0

func HttpGET(url string) ([]byte, error)

func IntArrContains

func IntArrContains(a []int64, x int64) bool

IntArrContains indicates whether x is contained in a.

func IntArrFind

func IntArrFind(a []int64, x int64) int

IntArrFind find element index in arr. -1 if not found

func IsFileExists added in v1.1.0

func IsFileExists(filepath string) bool

IsFileExists - check file exists

func MD5 added in v1.4.0

func MD5(val []byte) string

MD5 - calc MD5 checksum

func ParseConfigFromEnv added in v1.8.2

func ParseConfigFromEnv(cfgPointer any) error

func ParseStructFromJSON added in v1.1.0

func ParseStructFromJSON(jsonBytes []byte, destinationPointer interface{}) error

func ParseStructFromJSONFile added in v1.1.0

func ParseStructFromJSONFile(filepath string, destinationPointer interface{}) error

func ParseStructFromYaml added in v1.8.1

func ParseStructFromYaml(jsonBytes []byte, destinationPointer interface{}) error

func ParseStructFromYamlFile added in v1.8.1

func ParseStructFromYamlFile(filepath string, destinationPointer interface{}) error

func PrintIntroMessage added in v1.3.0

func PrintIntroMessage(appName, cryptoAddress string, cryptoCoinTag ...string)

PrintIntroMessage - publishes a funny message in the log asking to donate to the developer

func PrintObject added in v1.9.2

func PrintObject(o any)

func ProcessConfig added in v1.10.0

func ProcessConfig(cfg any) error

ProcessConfig iterates through the fields of the structure and loads envconfig for each one.

func ReadFile

func ReadFile(filepath string) ([]byte, error)

ReadFile read file to string

func ReadFileLines added in v1.0.5

func ReadFileLines(filePath string) ([]string, error)

ReadFileLines - read file to lines

func ReadFileToBytes added in v1.1.0

func ReadFileToBytes(filepath string) ([]byte, error)

ReadFileToString read file to bytes

func ReadFileToString

func ReadFileToString(filepath string) (string, error)

ReadFileToString read file to string

func Reconnect added in v1.0.5

func Reconnect(task ReconnectTask)

Reconnect - setup reconnect

func RunInBackground

func RunInBackground()

RunInBackground - blocking method with no exit

func SaveStringToFile

func SaveStringToFile(filepath string, content string) error

SaveStringToFile save arbitrary string to file

func SaveStructToJSONFile

func SaveStructToJSONFile(i interface{}, filepath string) error

SaveStructToJSONFile save structure to JSON file

func SaveStructToJSONFileIndent added in v1.7.0

func SaveStructToJSONFileIndent(i interface{}, filepath string) error

SaveStructToJSONFileIndent save structure to JSON file

func SaveStructToYamlFile added in v1.8.0

func SaveStructToYamlFile(i interface{}, filepath string) error

SaveStructToYamlFile save structure to YAML file (in plain text)

func Ternary

func Ternary(statement bool, a, b interface{}) interface{}

Ternary operator. conditional operator usage example: var res = ternary(val > 0, "positive", "negative")

func WaitForAppFinish added in v1.9.0

func WaitForAppFinish()

Types

type ChannelWorker added in v1.0.6

type ChannelWorker struct {
	MaxCapacity          int
	Events               chan interface{}
	EventHandlerCallback func(event interface{})
	ProcessingEvents     bool
	IsFinished           bool
	// contains filtered or unexported fields
}

func NewChannelWorker added in v1.0.6

func NewChannelWorker(callback func(event interface{}), maxCapacity int) *ChannelWorker

func (*ChannelWorker) AddEvent added in v1.0.6

func (w *ChannelWorker) AddEvent(event interface{})

func (*ChannelWorker) GetMessagesAvailableCount added in v1.0.6

func (w *ChannelWorker) GetMessagesAvailableCount() int

func (*ChannelWorker) SetAsync added in v1.2.0

func (w *ChannelWorker) SetAsync(asyncMode bool) *ChannelWorker

func (*ChannelWorker) Start added in v1.0.6

func (w *ChannelWorker) Start()

Start handle events NOTE: it's blocking method

func (*ChannelWorker) Stop added in v1.0.6

func (w *ChannelWorker) Stop()

type ConnFunc added in v1.0.5

type ConnFunc func() error

ConnFunc - some func

type DBConfig added in v1.10.0

type DBConfig struct {
	Host          string `envconfig:"DB_HOST" default:"localhost"`
	Port          int    `envconfig:"DB_PORT" default:"3306"`
	Name          string `envconfig:"DB_NAME"`
	User          string `envconfig:"DB_USER"`
	Password      string `envconfig:"DB_PASSWORD"`
	ConnTimeoutMS int    `envconfig:"DB_CONN_TIMEOUT" default:"5000"`

	MaxOpenConns        int `envconfig:"DB_MAX_OPEN_CONNS" default:"10"`
	MaxIdleConns        int `envconfig:"DB_MAX_IDLE_CONNS" default:"5"`
	ConnMaxLifetimeMins int `envconfig:"DB_CONN_MAX_LIFETIME_MINS" default:"5"`

	GormDebugMode bool `envconfig:"DB_GORM_DEBUG_MODE" default:"false"`
}

type ErrorFunc added in v1.0.1

type ErrorFunc func() error

ErrorFunc - func with error result

type RMQConfig added in v1.10.0

type RMQConfig struct {
	UseTLS   bool   `envconfig:"AMQP_USE_TLS" default:"false"`
	Host     string `envconfig:"AMQP_HOST" default:"localhost"`
	Port     int    `envconfig:"AMQP_PORT" default:"5672"`
	User     string `envconfig:"AMQP_USER"`
	Password string `envconfig:"AMQP_PASSWORD"`
}

type ReconnectTask added in v1.0.5

type ReconnectTask struct {
	ConnectionDescription      string // connection name
	ReconnectionAttemptsNumber int
	ConnectionTimeout          time.Duration
	ReconnectAfterTimeout      time.Duration
	WaitingBetweenAttempts     time.Duration

	// callbacks
	ConnCallback  ConnFunc
	ErrorCallback func(error)
	LogCallback   func(string)
}

type RedisConfig added in v1.10.0

type RedisConfig struct {
	Host     string `envconfig:"REDIS_HOST" default:"0.0.0.0"`
	Port     string `envconfig:"REDIS_PORT" default:"6379"`
	Password string `envconfig:"REDIS_PASSWORD" default:""`
}

type SentryConfig added in v1.10.0

type SentryConfig struct {
	DSN           string  `envconfig:"SENTRY_DSN"`
	EnableTracing bool    `envconfig:"SENTRY_ENABLE_TRACING" default:"true"`
	SampleRate    float64 `envconfig:"SENTRY_SAMPLE_RATE" default:"0.2"`
}

Jump to

Keyboard shortcuts

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