Giter Club home page Giter Club logo

gohelper's Introduction

Go Helper

This repository provides some handy helper function. Generics are heavily used for this purpose.

Provided Functions

boolean Functions

boolean.In

The In function is a shorthand for multiple OR conditions.

Example:

package main

import (
	"fmt"
	"github.com/skroczek/gohelper/boolean"
)

func main() {
    v := "1"
    // Alternative: if v == "2" || v == "3" ...
	if boolean.In(v, "2", "3" /*, ... */) {
		fmt.Printf("The first argument occurs at least once more in the remaining ones.")
	} else {
		fmt.Printf("The first argument does not occur again")
    }
}

boolean.NotIn

The NotIn function is a shorthand for multiple AND NOT EQUALS conditions.

Example:

package main

import (
	"fmt"
	"github.com/skroczek/gohelper/boolean"
)

func main() {
    v := "1"
    // Alternative: if v != "2" && v != "3" ...
	if boolean.NotIn(v, "2", "3" /*, ... */) {
		fmt.Printf("The first argument does not occur again")
	} else {
		fmt.Printf("The first argument occurs at least once more in the remaining ones.")
    }
}

diff Functions

diff.Diff

The Diff function returns the elements of two slices that have been added or removed. The first slice is considered the "old" and the second the "new".

diff.Added

The Added function returns the elements of two slices that occur in the second slice but not in the first.

diff.Removed

The Removed function returns the elements of two slices that occur in the first slice but not in the second.

error Functions

error.Panic

Triggers a panic when err != nil

error.Must

Triggers a panic if the err != nil, otherwise only the first return value is returned. The second (error) return value is not returned.

error.Must2

Triggers a panic if err != nil, otherwise only the first two return values are returned. The third (error) return value is not returned.

interfaces

interfaces.Equaler

The Equaler interface allows for the general comparison of two (same) types. It is mainly used for the diff.Diff function.

Example:

package main

import "fmt"

type String string

func (s String) Equal(v String) bool {
	return s == v
}

func main() {

	a := String("a")
	b := String("b")

	if a.Equal(b) {
		fmt.Printf("%s == %s", a, b)
	}else{
		fmt.Printf("%s !== %s", a, b)
    }// Output: a !== b
}

ptr

ptr.Ptr

The function ptr.Prt is a shortcut for creating a pointer on the fly. Useful mainly for native types like int, float64, string, etc.

package main

import "github.com/skroczek/gohelper/ptr"

func main() {
	_ = struct {
		a *int
		b *string
		c *float32
		d *float64
		//...
	}{
		a: ptr.Ptr(1),
		b: ptr.Ptr("a"),
		c: ptr.Ptr(float32(21)),
        d: ptr.Ptr(23.42),
        // ...
	}
}

Test assert Functions

assert.Equal

Tests whether two values are equal. If the test fails, an error is generated with a corresponding message. Under the hood, the function reflect.DeepEqual is used.

assert.NotEqual

Tests whether two values are not equal. If the test fails, an error is generated with a corresponding message. Under the hood, the function reflect.DeepEqual is used.

License

Copyright 2023 Sebastian Kroczek [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

gohelper's People

Contributors

skroczek 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.