Skip to content

Commit 22f1fe6

Browse files
committed
增加 params 格式化 msg 的功能
1 parent 9a4cf81 commit 22f1fe6

5 files changed

Lines changed: 24 additions & 4 deletions

File tree

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.2.1
4+
5+
> 此版本发布于 2023-04-24
6+
7+
* 增加 params 格式化 msg 的功能
8+
39
### v0.2.0
410

511
> 此版本发布于 2023-03-13

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/FishGoddess/errors
22

3-
go 1.15
3+
go 1.17

option.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package errors
66

7+
import "fmt"
8+
79
type Option func(e *Error)
810

911
func (o Option) Apply(e *Error) {
@@ -17,8 +19,12 @@ func applyOptions(e *Error, opts []Option) {
1719
}
1820

1921
// WithMsg sets msg to e.
20-
func WithMsg(msg string) Option {
22+
func WithMsg(msg string, params ...interface{}) Option {
2123
return func(e *Error) {
24+
if len(params) > 0 {
25+
msg = fmt.Sprintf(msg, params...)
26+
}
27+
2228
e.msg = msg
2329
}
2430
}

option_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ func TestWithMsg(t *testing.T) {
3333
if got.String() != expect.String() {
3434
t.Errorf("got %s != expect %s", got.String(), expect.String())
3535
}
36+
37+
got = &Error{msg: ""}
38+
expect = &Error{msg: "ok123"}
39+
40+
WithMsg("%s%d", "ok", 123)(got)
41+
if got.String() != expect.String() {
42+
t.Errorf("got %s != expect %s", got.String(), expect.String())
43+
}
3644
}

0 commit comments

Comments
 (0)