foreach

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

run a function for each ...

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Array added in v0.15.0

func Array(arr interface{}, handler func(int, interface{}, int))

Array runs a function for each element of an array

func Dir added in v0.15.0

func Dir(dirname string, recursive bool, fnc func(string, string, string, bool, os.FileInfo)) error

Dir runs a function for each file in a directory

func File

func File(dirname string, recursive bool, fnc func(string, string, string, bool, os.FileInfo)) error

File runs a function for each file in a directory

func JSON

func JSON(str string, handler func(*string, *int, *interface{}, int)) error

JSON runs a function for each element of a JSON string

func Struct

func Struct(sstruct interface{}, handler func(string, string, interface{}, int))

Struct runs a function for each element of a struct

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/foreach"
)

type Foobar struct {
	X int
	Y int
}
type Foo struct {
	Id    int
	Name  string
	Lorem Foobar
	Ipsum interface{}
	Query func()
}

func main() {
	foreach.Struct(Foo{
		Id:   5,
		Name: "Foobar",
		Lorem: Foobar{
			X: 42,
			Y: 23,
		},
		Ipsum: float64(2),
		Query: func() {
			fmt.Println("---")
		},
	}, func(key string, index string, value interface{}, depth int) {
		fmt.Printf("name: %v, type: %v, depth: %v\n", key, index, depth)
	})

}
Output:

name: Id, type: int, depth: 0
name: Name, type: string, depth: 0
name: Lorem, type: foreach_test.Foobar, depth: 0
name: X, type: int, depth: 1
name: Y, type: int, depth: 1
name: Ipsum, type: interface {}, depth: 0
name: Query, type: func(), depth: 0

func XML added in v0.15.0

func XML(str string, handler func(*string, *int, *interface{}, int)) error

XML runs a function for each element of a XML string

Types

This section is empty.

Jump to

Keyboard shortcuts

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