Skip to content

Commit

Permalink
新文件: dict.go
Browse files Browse the repository at this point in the history
	新文件:   format.go
	新文件:   format_test.go
	修改:     go.mod
  • Loading branch information
ssst0n3 committed Sep 30, 2020
1 parent 31500e8 commit 4e34ea6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package awesome_libs

type Dict map[string]interface{}
17 changes: 17 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package awesome_libs

import (
"bytes"
"github.com/ssst0n3/awesome_libs/awesome_error"
"text/template"
)

func Format(tpl string, arg Dict) string {
var msg bytes.Buffer

t, err := template.New("awesome").Parse(tpl)
awesome_error.CheckFatal(err)

awesome_error.CheckFatal(t.Execute(&msg, arg))
return msg.String()
}
15 changes: 15 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package awesome_libs

import (
"github.com/go-playground/assert/v2"
"testing"
)

func TestFormat(t *testing.T) {
msg := Format(
"Hello {{.name}}", Dict{
"name": "awesome",
},
)
assert.Equal(t, "Hello awesome", msg)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/assert/v2 v2.0.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.4.0
Expand Down

0 comments on commit 4e34ea6

Please sign in to comment.