Skip to content
/ x Public

🔨 Tools like Laravel Collection or lodash.js

Notifications You must be signed in to change notification settings

bygo/x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

English | 简体中文

X

🔨 Tools like Laravel Collection or lodash.js

Installation

go get github.com/bygo/x

Getting Started

package main

import (
	"fmt"
	"github.com/bygo/x"
)

func main() {
	var nums = []int{1, 2, 3, 4, 5}
	nums = x.Collect(nums).
		Filter(func(val int, k int) bool {
			return val%2 == 1 // []int{1, 3, 5}
		}).
		Diff([]int{1}). // []int{3, 5}
		Map(func(val int, k int) int {
			if val == 3 {
				return val
			}
			return val * 2 // []int{3, 10}
		}).
		Replace(3, 5, 1). // Replace(old, new, n) => []int(5, 10)
		ForEach(func(val int, k int) {
			println(val) // Output 5,10
		}).
		ToSlice()

	fmt.Printf("%+v", nums) // Output []int{5,10}
}

Releases

No releases published

Packages

No packages published

Languages