Skip to content

Commit

Permalink
feat: 封装uuid函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zionjxyu committed May 16, 2022
1 parent 2de772d commit cfce272
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
22 changes: 21 additions & 1 deletion collection/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package collection

import "reflect"
import (
"reflect"

"github.com/mitchellh/mapstructure"
)

// InsertStringMap 插入map的简单封装,主要是判断map是否是空的,如果为空就构造一个map
func InsertStringMap(srcMap map[string]string, key, value string) map[string]string {
Expand All @@ -27,3 +31,19 @@ func StructToMap(stu interface{}) map[string]interface{} {

return data
}

// MapToStruct 强类型map转换struct
func MapToStruct(input interface{}, output interface{}, tags ...string) error {
tag := ""
if len(tags) >= 1 {
tag = tags[0]
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: tag,
Result: output,
})
if err != nil {
return err
}
return decoder.Decode(input)
}
9 changes: 9 additions & 0 deletions common/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

import uuid "github.com/satori/go.uuid"

// MustUUID 创建一个UUID,如果有错误,则抛出panic
func MustUUID() string {
u := uuid.NewV4()
return u.String()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ require (
github.com/gin-gonic/gin v1.7.7
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/olivere/elastic/v7 v7.0.32
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.19.0 // indirect
github.com/robfig/cron v1.2.0
github.com/satori/go.uuid v1.2.0
go.etcd.io/etcd/client/v3 v3.5.3
golang.org/x/text v0.3.7
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -182,6 +184,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
Expand Down

0 comments on commit cfce272

Please sign in to comment.