lodago

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: MIT Imports: 8 Imported by: 0

README

Lodago

It has the same goal as the lodash library, providing rich functions for golang.

Function

  • UUID - Generate the uuid string
  • Str2MD5 - Converts the string to md5
  • Find - Find a specific element in sclice
  • Filter - Filter a slice
  • Shuffle - Shuffle a slice
  • SelectFields - Output json based on the selected field
  • DropFields - Output json based on the drop field
  • Struct2Map - Converts the struct to map
  • Map2Struct - Converts the map to struct
  • Map2JSON - Converts the map to json
  • JSON2Map - Converts the json to map

Demo

  • UUID
func main() {
	fmt.Println(lodago.UUID())
}

result

9de5f29c-b744-4b3e-b0c6-b5c1e3705510
  • Str2MD5
func main() {
    str := []byte("test12345")
    fmt.Println(lodago.Str2MD5(str))
}

result

c06db68e819be6ec3d26c6038d8e8d1f
  • Find
type User struct {
	ID            int
	Name          string
}

func main() {
	users := []User {
		User{1, "张三"},
		User{2, "李四"},
		User{3, "赵四"},
	}

	findID := 2

	user, isFound := lodago.Find(users, func(ele interface{}, index int) bool {
		return ele.(User).ID == findID
	})
	if isFound {
		fmt.Println(user.(User))
	}
}

result

{2 李四}
  • Filter
type User struct {
	ID   int
	Name string
}

func main() {
	users := []User{
		User{1, "张三"},
		User{2, "李四"},
		User{3, "赵四"},
	}

	id := 2

	filterUsers, isFound := lodago.Filter(users, func(ele interface{}, index int) bool {
		return ele.(User).ID != id
	})
	if isFound {
		fmt.Println(filterUsers.([]User))
	}
}

result

[{1 张三} {3 赵四}]
  • Shuffle
func main() {
	array := []string{"a", "b", "c", "d", "e", "f"}
	fmt.Println("打乱前:", array)
	lodago.Shuffle(array)
	fmt.Println("打乱后:", array)
}

result

打乱前: [a b c d e f]
打乱后: [b c f a d e]
  • SelectFields
type SearchResult struct {
	Date     string `json:"date"`
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Password string `json:"password"`
}

func main() {
	result := SearchResult{
		Date:     "2020-03-30",
		ID:       1,
		Name:     "张三",
		Password: "123456",
	}
	fields := []string{"name", "id"}
	out, _ := lodago.SelectFields(&result, fields...)
	fmt.Println(string(out))
}

result

{"id":1,"name":"张三"}
  • DropFields
type SearchResult struct {
	Date     string `json:"date"`
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Password string `json:"password"`
}

func main() {
	result := SearchResult{
		Date:     "2020-03-30",
		ID:       1,
		Name:     "张三",
		Password: "123456",
	}
    fields := []string{"password"}
	out, _ := lodago.DropFields(&result, fields...)
	fmt.Println(string(out))
}

result

{"date":"2020-03-30","id":1,"name":"张三"}
  • Struct2Map
type SearchResult struct {
	Date     string `json:"date"`
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Password string `json:"password"`
}

func main() {
	result := SearchResult{
		Date:     "2020-03-30",
		ID:       1,
		Name:     "张三",
		Password: "123456",
	}
	mapValues := lodago.Struct2Map(result)
	fmt.Println(mapValues)
}

result

map[Date:2020-03-30 ID:1 Name:张三 Password:123456]
  • Map2Struct
type SearchResult struct {
	Date     string `json:"date"`
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Password string `json:"password"`
}

func main() {
	result := SearchResult{
		Date:     "2020-03-30",
		ID:       1,
		Name:     "张三",
		Password: "123456",
	}
	mapValues := lodago.Struct2Map(result)

	var obj SearchResult
	lodago.Map2Struct(mapValues, &obj)
	fmt.Println(obj)
}

result

{2020-03-30 1 张三 123456}
  • Map2JSON
func main() {
	mapValues := map[string]interface{}{
		"id":   1,
		"name": "peter",
	}
	jsonStr, err := lodago.Map2JSON(mapValues)
	if err == nil {
		fmt.Println(jsonStr)
	}
}

result

{"id":1,"name":"peter"}
  • JSON2Map
func main() {
	mapValues := map[string]interface{}{
		"id":   1,
		"name": "peter",
	}
	jsonStr, err := lodago.Map2JSON(mapValues)
	if err == nil {
		mapValues, err := lodago.JSON2Map([]byte(jsonStr))
		if err == nil {
			fmt.Println(mapValues)
		}
	}
}

result

map[id:1 name:peter]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DropFields

func DropFields(v interface{}, fields ...string) ([]byte, error)

DropFields 选择性的抛弃struct的一些字段

func Filter

func Filter(arr interface{}, callback func(ele interface{}, index int) bool) (interface{}, bool)

Filter 切片元素过滤

func Find

func Find(arr interface{}, callback func(ele interface{}, index int) bool) (interface{}, bool)

Find 数组查找函数

func JSON2Map

func JSON2Map(jsonStr []byte) (map[string]interface{}, error)

JSON2Map json字符串转换成map容器

func Map2JSON

func Map2JSON(mapValue map[string]interface{}) (string, error)

Map2JSON map容器转换成json字符串

func Map2Struct

func Map2Struct(mapValue map[string]interface{}, obj interface{}) error

Map2Struct 将map对象转换成结构体

func SelectFields

func SelectFields(v interface{}, fields ...string) ([]byte, error)

SelectFields 针对性的选择struct字段输出

func Shuffle

func Shuffle(arr interface{})

Shuffle 打乱数组

func Str2MD5

func Str2MD5(str []byte) string

Str2MD5 字符串转换成md5加密

func Struct2Map

func Struct2Map(obj interface{}) map[string]interface{}

Struct2Map 利用反射将结构体转化为map

func UUID

func UUID() string

UUID 生成唯一的uuid

Types

This section is empty.

Jump to

Keyboard shortcuts

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