Code Monkey home page Code Monkey logo

go-recursive's Introduction

Go-recursive - Walk and update nested Go objects

Go Reference Coverage Status

Go-recursive is Golang library to walk through and update nested objects. It goes down structs, maps, slices recursively.

Example

Incrememt Int value inside nested struct:

  1. Define function:

    func incrementFunc(obj interface{}, level int) interface{} {
    	if i, ok := obj.(int); ok {
    		i += +1
    		return i
    	}
    
    	return obj // return unchanged
    }
  2. Define object. Let it be complex object here - nested structs inside map:

    type NestedStruct struct {
    	Int    int
    	Nested *NestedStruct
    }
    
    var(
    	n = NestedStruct{Int: 20}
    	obj = map[int]NestedStruct{
    		1: NestedStruct{Int: 10, Nested: &n},
    		2: NestedStruct{},
    	}
    )
  3. Execute:

    	recursive.Go(&obj, incrementFunc)
  4. Check result:

    Before:
    map[1:{Int:10 Nested:0xc000010230} 2:{Int:0 Nested:<nil>}] (map)
    >>>> {Int:0 Nested:<nil>} (struct)
    >>>>>>>> 0 (int)
    >>>>>>>> <nil> (ptr)
    >>>> {Int:10 Nested:0xc000010230} (struct)
    >>>>>>>> 10 (int)
    >>>>>>>> &{Int:20 Nested:<nil>} (ptr)
    >>>>>>>>>>>> {Int:20 Nested:<nil>} (struct)
    >>>>>>>>>>>>>>>> 20 (int)
    >>>>>>>>>>>>>>>> <nil> (ptr)
    
    After:
    map[1:{Int:11 Nested:0xc000010230} 2:{Int:1 Nested:<nil>}] (map)
    >>>> {Int:1 Nested:<nil>} (struct)
    >>>>>>>> 1 (int)
    >>>>>>>> <nil> (ptr)
    >>>> {Int:11 Nested:0xc000010230} (struct)
    >>>>>>>> 11 (int)
    >>>>>>>> &{Int:21 Nested:<nil>} (ptr)
    >>>>>>>>>>>> {Int:21 Nested:<nil>} (struct)
    >>>>>>>>>>>>>>>> 21 (int)
    >>>>>>>>>>>>>>>> <nil> (ptr)

Check examples directory for complete program.

go-recursive's People

Contributors

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