1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2022-05-12 10:03:07 +08:00
parent af7029133f
commit 3dc3fbb0f2
2 changed files with 17 additions and 14 deletions

View File

@ -1,6 +1,10 @@
package maps
import "golang.org/x/exp/constraints"
import (
"strings"
"golang.org/x/exp/constraints"
)
type Map[K constraints.Ordered, V any] interface {
Set(key K, value V) // 设置值
@ -32,3 +36,14 @@ func Merge[K comparable, V any](mm ...map[K]V) map[K]V {
return ret
}
// 按照键值生成字符串
func Join[K constraints.Ordered, V any](m Map[K, V], sep string, f func(k K, v V) string) string {
slice := make([]string, 0, m.Count())
for _, k := range m.Keys() {
v, _ := m.Get(k)
slice = append(slice, f(k, v))
}
return strings.Join(slice, sep)
}