Skip to content

go-zwbc/slicezh

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

slicezh

Chinese-named package providing common slice operations with intuitive APIs


CHINESE README

中文说明

Main Features

🎯 Chinese Function Names: Intuitive Chinese-named slice operations 🔄 Filtering & Finding: Filtering elements and finding first match 🔀 Transform: Map elements to new types 🎲 Random: Random element selection and shuffling 📋 Deduplicate: Remove duplicates while preserving sequence 🗺️ Slice to Map: Converts slice to map with custom keyFunc

Installation

go get github.com/go-zwbc/slicezh

Usage

Basic Slice Operations

This example demonstrates filtering, finding, reversing, transforming, and deduplicating slices.

package main

import (
	"fmt"

	"github.com/go-zwbc/slicezh"
)

func main() {
	// Filtering slice
	nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	evens := slicezh.Get按条件过滤(nums, func(v int) bool {
		return v%2 == 0
	})
	fmt.Println("Even numbers:", evens) // [2 4 6 8 10]

	// Finding first match
	first, ok := slicezh.Get首个满足条件的(nums, func(v int) bool {
		return v > 5
	})
	fmt.Println("First > 5:", first, ok) // 6 true

	// Contains checking
	fmt.Println("Contains 5:", slicezh.Contains(nums, 5)) // true
	fmt.Println("In slice:", slicezh.In(5, nums))         // true

	// Reversing slice
	strs := []string{"a", "b", "c", "d"}
	reversed := slicezh.Get获取逆序新数组(strs)
	fmt.Println("Reversed:", reversed) // [d c b a]

	// Transforming slice
	doubled := slicezh.Get映射转换(nums[:5], func(v int) int {
		return v * 2
	})
	fmt.Println("Doubled:", doubled) // [2 4 6 8 10]

	// Removing duplicates
	dups := []int{1, 2, 2, 3, 1, 4, 3, 5}
	unique := slicezh.Get元素去重(dups)
	fmt.Println("Unique:", unique) // [1 2 3 4 5]

	// Slice to map
	type Person struct {
		Name string
		Age  int
	}
	people := []Person{
		{Name: "Alice", Age: 20},
		{Name: "Bob", Age: 25},
	}
	peopleMap := slicezh.Get映射Map(people, func(p Person) string {
		return p.Name
	})
	fmt.Println("People map:", peopleMap)

	// Random element
	random := slicezh.Get随机取个元素(nums)
	fmt.Println("Random element:", random)
}

⬆️ Source: Source

Transform and Mapping Operations

This example shows advanced transformation and mapping with structs.

package main

import (
	"fmt"

	"github.com/go-zwbc/slicezh"
)

func main() {
	// Transform int to string
	nums := []int{1, 2, 3, 4, 5}
	strs := slicezh.Get映射转换(nums, func(n int) string {
		return fmt.Sprintf("num-%d", n)
	})
	fmt.Println("Transformed:", strs) // [num-1 num-2 num-3 num-4 num-5]

	// Transform struct to value
	type Product struct {
		Name  string
		Price float64
	}
	products := []Product{
		{Name: "Apple", Price: 1.5},
		{Name: "Banana", Price: 0.8},
		{Name: "Orange", Price: 2.0},
	}

	// Extract names
	names := slicezh.Get映射转换(products, func(p Product) string {
		return p.Name
	})
	fmt.Println("Product names:", names) // [Apple Banana Orange]

	// Slice to map with struct
	productMap := slicezh.Get映射Map(products, func(p Product) string {
		return p.Name
	})
	fmt.Println("Product map:", productMap)

	// Chain operations: filtering then transforming
	filtered := slicezh.Get按条件过滤(products, func(p Product) bool {
		return p.Price > 1.0
	})
	expensive := slicezh.Get映射转换(filtered, func(p Product) string {
		return fmt.Sprintf("%s ($%.2f)", p.Name, p.Price)
	})
	fmt.Println("Expensive items:", expensive) // [Apple ($1.50) Orange ($2.00)]
}

⬆️ Source: Source

API Reference

Function Description (EN) 描述 (ZH)
Get按条件过滤 Returns elements matching condition 按条件过滤切片元素
Get首个满足条件的 Finds first element matching condition 返回第一个满足条件的元素
Mut翻转自身内容 Reverses slice in place 原地翻转切片内容
Get获取逆序新数组 Returns new reversed slice 返回逆序的新切片
Get随机取个元素 Gets random element 随机取一个元素
Mut随机乱序元素 Shuffles slice in place 原地随机打乱切片
Contains Checks if slice contains value 判断切片是否包含值
In Checks if value is in slice 判断值是否在切片中
Get映射Map Converts slice to map 将切片转换为 map
Get映射转换 Transforms elements to new type 将元素转换为新类型
Get元素去重 Removes duplicates preserving sequence 去重并保持原始顺序

Examples

Random and Mutation Operations

Shuffling slice in place:

cards := []string{"A", "2", "3", "4", "5"}
slicezh.Mut随机乱序元素(cards)

Reversing slice in place:

sequence := []int{1, 2, 3, 4, 5}
slicezh.Mut翻转自身内容(sequence)
// sequence is now [5, 4, 3, 2, 1]

Getting reversed new slice without changing the input:

letters := []string{"a", "b", "c"}
reversed := slicezh.Get获取逆序新数组(letters)
// letters unchanged: [a b c]
// reversed: [c b a]

⬆️ Source: Source

Naming Conventions

  • Get prefix: Returns new value, does not change the input slice
  • Mut prefix: Modifies the slice in place

License

MIT License - see LICENSE file.

About

Go语言的 slice 操作函数 以中文编写 使用中文造车轮

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published