Skip to content

Commit 7a37e25

Browse files
committed
Update example
1 parent b66a22d commit 7a37e25

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

internal/examples/basic/convert/struct_to_str.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,41 @@ import (
66
"github.com/devlights/gomy/output"
77
)
88

9-
type sts struct {
10-
id int
11-
name string
12-
}
9+
type (
10+
sts struct {
11+
id int
12+
name string
13+
}
14+
15+
yen int
16+
)
1317

1418
func (s sts) String() string {
1519
return fmt.Sprintf("id=%d,name=%s", s.id, s.name)
1620
}
1721

22+
func (y yen) String() string {
23+
return fmt.Sprintf("¥%d", y)
24+
}
25+
1826
// StructToStr は、fmt.Sprint() を利用して 構造体 を 文字列 にするサンプルです.
1927
func StructToStr() error {
20-
21-
s1 := sts{
22-
id: 100,
23-
name: "sts",
24-
}
25-
26-
s := fmt.Sprint(s1)
27-
output.Stdoutf("[struct to str]", "%[1]v(%[1]T) --> %[2]q(%[2]T)\n", s1, s)
28+
var (
29+
s1 = sts{
30+
id: 100,
31+
name: "sts",
32+
}
33+
y = yen(1000)
34+
)
35+
36+
var (
37+
fn = func(o fmt.Stringer) {
38+
output.Stdoutf("[struct to str]", "%[1]v(%[1]T) --> %[2]q(%[2]T)\n", o, fmt.Sprint(o))
39+
}
40+
)
41+
42+
fn(s1)
43+
fn(y)
2844

2945
return nil
3046
}

0 commit comments

Comments
 (0)