From 4e34ea6e13fe0ce5daf5aa72dfa1a624b03aa55e Mon Sep 17 00:00:00 2001 From: st0n3 Date: Wed, 30 Sep 2020 13:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20?= =?UTF-8?q?=20dict.go=20=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20fo?= =?UTF-8?q?rmat.go=20=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20forma?= =?UTF-8?q?t=5Ftest.go=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20go.m?= =?UTF-8?q?od?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dict.go | 3 +++ format.go | 17 +++++++++++++++++ format_test.go | 15 +++++++++++++++ go.mod | 1 + 4 files changed, 36 insertions(+) create mode 100644 dict.go create mode 100644 format.go create mode 100644 format_test.go diff --git a/dict.go b/dict.go new file mode 100644 index 0000000..10f82cb --- /dev/null +++ b/dict.go @@ -0,0 +1,3 @@ +package awesome_libs + +type Dict map[string]interface{} diff --git a/format.go b/format.go new file mode 100644 index 0000000..9372681 --- /dev/null +++ b/format.go @@ -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() +} diff --git a/format_test.go b/format_test.go new file mode 100644 index 0000000..6a6c84e --- /dev/null +++ b/format_test.go @@ -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) +} diff --git a/go.mod b/go.mod index cab0f5d..8237cb3 100644 --- a/go.mod +++ b/go.mod @@ -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