Giter VIP home page Giter VIP logo

dry's Introduction

dry - DRY Go Library for Common Functions

Overview

dry is a Go library providing a collection of common generic functions for slices and maps, designed to promote the DRY (Don't Repeat Yourself) principle in Go programming. This library aims to reduce repetitive code patterns by offering reusable, generic implementations for frequently needed operations.

Installation

To use dry in your Go project, install it using go get:

go get github.com/jon-ski/dry

Usage

dry provides generic functions for operations on slices and maps. Here's how you can use these functions in your Go code:

Examples

import "github.com/jon-ski/dry"

Slices

package main

import (
    "fmt"
    "github.com/jon-ski/dry"
)

func main() {
    intSlice := []int{1, 2, 3, 4, 5}
    fmt.Printf("Initial: %+v\n", intSlice)
    // Filter out even numbers
    intSlice = dry.Filter(intSlice, func(i int) bool {
        return i%2 != 0
    })
    fmt.Printf("Filtered: %+v\n", intSlice)
}

Maps

package main

import (
    "fmt"
    "github.com/jon-ski/dry"
)

func main() {
    // Create a map[string]interface{} with some data
    m := map[string]interface{}{
        "name": "John Doe",
        "age":  42,
        "interests": "golang",
    }
    fmt.Printf("Initial: %+v\n", m)

    // Interests is a string, but we want a slice of strings
    // We can use the Map function to convert it
    m = dry.MapKeyFunc(m, "interests", func(i interface{}) interface{} {
        switch v := i.(type) {
        case string:
            return []string{v}
        case []string:
            return v
        default:
            return []string{}
        }
    })
    m["interests"] = append(m["interests"].([]string), "cycling")
    fmt.Printf("Updated: %+v\n", m)
}

Function Documentation

Each function in dry is documented with comments explaining its purpose, parameters, and return values. For detailed documentation, see the source code or use godoc.

License

dry is licensed under the MIT License.

dry's People

Contributors

jon-ski 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.