Skip to content

Commit 782c23c

Browse files
authored
Merge pull request #769 from devlights/add-results-in-example
2 parents a3028b1 + edb7a33 commit 782c23c

12 files changed

+276
-12
lines changed

examples/basic/structs/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
|------------------------------|-----------------------------------------|-------------------------------------------------------------------|
7-
| struct\_basic01.go | struct\_basic01 | 構造体についてのサンプル |
8-
| struct\_basic02.go | struct\_basic02 | 構造体についてのサンプル |
9-
| struct\_basic03.go | struct\_basic03 | 構造体についてのサンプル |
10-
| struct\_basic04.go | struct\_basic04 | 構造体についてのサンプル |
11-
| struct\_anonymous\_struct.go | struct\_anonymous\_struct | 匿名構造体についてのサンプルです。 |
12-
| struct\_empty\_struct.go | struct\_empty\_struct | 空の構造体についサンプルです. |
13-
| struct\_deepequal.go | struct\_deep\_equal | 構造体に対して reflect.DeepEqual() した場合のサンプルです. |
14-
| struct\_blank\_identifier.go | struct\_blank\_identifier | 構造体定義時に blank identifier を意図的に用意して初期化時にフィールド名の指定を必須にするやり方のサンプルです. |
15-
| struct\_same\_method | struct\_same\_method\_on\_each\_type.go | レシーバの型が異なる同名メソッド定義のサンプルです |
16-
| struct\_memory\_padding.go | struct\_memory\_padding | 構造体メンバーの定義順によってGoランタイムがメモリ上にパディングを挿入することを確認するサンプルです |
5+
| file | example name | note |
6+
| -------------------------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- |
7+
| struct_basic01.go | struct_basic01 | 構造体についてのサンプル |
8+
| struct_basic02.go | struct_basic02 | 構造体についてのサンプル |
9+
| struct_basic03.go | struct_basic03 | 構造体についてのサンプル |
10+
| struct_basic04.go | struct_basic04 | 構造体についてのサンプル |
11+
| struct_anonymous_struct.go | struct_anonymous_struct | 匿名構造体についてのサンプルです。 |
12+
| struct_empty_struct.go | struct_empty_struct | 空の構造体についサンプルです. |
13+
| struct_deepequal.go | struct_deep_equal | 構造体に対して reflect.DeepEqual() した場合のサンプルです. |
14+
| struct_blank_identifier.go | struct_blank_identifier | 構造体定義時に blank identifier を意図的に用意して初期化時にフィールド名の指定を必須にするやり方のサンプルです. |
15+
| struct_same_method | struct_same_method_on_each_type.go | レシーバの型が異なる同名メソッド定義のサンプルです |
16+
| struct_memory_padding.go | struct_memory_padding | 構造体メンバーの定義順によってGoランタイムがメモリ上にパディングを挿入することを確認するサンプルです |

examples/basic/structs/struct_anonymous_struct.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ func StructAnonymousStruct() error {
2828
fmt.Printf("[anonStructSlice] %#v\n", anonStructSlice)
2929

3030
return nil
31+
32+
/*
33+
$ task
34+
task: [build] go build .
35+
task: [run] ./try-golang -onetime
36+
37+
ENTER EXAMPLE NAME: struct_anonymous_struct
38+
39+
[Name] "struct_anonymous_struct"
40+
[anonStruct] struct { x int; y int }{x:100, y:200}
41+
[anonStructSlice] []struct { x int; y int }{struct { x int; y int }{x:100, y:200}, struct { x int; y int }{x:300, y:400}}
42+
43+
44+
[Elapsed] 21.84µs
45+
*/
46+
3147
}

examples/basic/structs/struct_basic01.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,28 @@ func Basic01() error {
6161
pf("Method2: %d(%T)\n", st02.Method2(), st02) // -> 110
6262

6363
return nil
64+
65+
/*
66+
$ task
67+
task: Task "build" is up to date
68+
task: [run] ./try-golang -onetime
69+
70+
ENTER EXAMPLE NAME: struct_basic01
71+
72+
[Name] "struct_basic01"
73+
Method1: 30(*structs.MyStruct)
74+
Method2: 60(*structs.MyStruct)
75+
Method1: 55(structs.MyStruct)
76+
Method2: 110(structs.MyStruct)
77+
78+
79+
Method1: 40(*structs.MyStruct)
80+
Method2: 80(*structs.MyStruct)
81+
Method1: 55(structs.MyStruct)
82+
Method2: 110(structs.MyStruct)
83+
84+
85+
[Elapsed] 69.231µs
86+
*/
87+
6488
}

examples/basic/structs/struct_basic02.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,21 @@ func Basic02() error {
3232
fmt.Printf("%#v\n", isNil)
3333

3434
return nil
35+
36+
/*
37+
$ task
38+
task: [build] go build .
39+
task: [run] ./try-golang -onetime
40+
41+
ENTER EXAMPLE NAME: struct_basic02
42+
43+
[Name] "struct_basic02"
44+
structs.intPair{x:0, y:0, s:"", sl:[]int(nil)}
45+
[]int(nil) is nil? ==> true
46+
true
47+
48+
49+
[Elapsed] 32.46µs
50+
*/
51+
3552
}

examples/basic/structs/struct_basic03.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,19 @@ func Basic03() error {
4141
fmt.Println(a, b)
4242

4343
return nil
44+
45+
/*
46+
$ task
47+
task: [build] go build .
48+
task: [run] ./try-golang -onetime
49+
50+
ENTER EXAMPLE NAME: struct_basic03
51+
52+
[Name] "struct_basic03"
53+
{{100 A} val-a} {{200 B} val-b}
54+
55+
56+
[Elapsed] 12.12µs
57+
*/
58+
4459
}

examples/basic/structs/struct_basic04.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,21 @@ func Basic04() error {
2727
fmt.Printf("%#v\n%#v\n%#v(%#v)\n", st01, st02, i01, *i01)
2828

2929
return nil
30+
31+
/*
32+
$ task
33+
task: [build] go build .
34+
task: [run] ./try-golang -onetime
35+
36+
ENTER EXAMPLE NAME: struct_basic04
37+
38+
[Name] "struct_basic04"
39+
&structs.mySt01{key:100, value:"hello world"}
40+
&structs.mySt01{key:200, value:"world hello"}
41+
(*int)(0xc000014958)(111)
42+
43+
44+
[Elapsed] 23.41µs
45+
*/
46+
3047
}

examples/basic/structs/struct_blank_identifier.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,19 @@ func BlankIdentifier() error {
3333
output.Stdoutl("result", o1, o3, o4)
3434

3535
return nil
36+
37+
/*
38+
$ task
39+
task: [build] go build .
40+
task: [run] ./try-golang -onetime
41+
42+
ENTER EXAMPLE NAME: struct_blank_identifier
43+
44+
[Name] "struct_blank_identifier"
45+
result {ok pattern {}} {ok pattern} {ok pattern}
46+
47+
48+
[Elapsed] 22.92µs
49+
*/
50+
3651
}

examples/basic/structs/struct_deepequal.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,59 @@ func StructDeepEqual() error {
157157
output.Stdoutl("[deepequal(st1,st2)]", reflect.DeepEqual(st1, st2))
158158

159159
return nil
160+
161+
/*
162+
$ task
163+
task: [build] go build .
164+
task: [run] ./try-golang -onetime
165+
166+
ENTER EXAMPLE NAME: struct_deep_equal
167+
168+
[Name] "struct_deep_equal"
169+
===> 等値かどうかのチェック
170+
[&st1] 0xc000024d60
171+
[&st2] 0xc000024d80
172+
[&st1 == &st2] false
173+
===> 等価かどうかのチェック (1)
174+
===> 全てのフィールドの値が同じ
175+
[st1] {{1 1} 1 0xc000014958}
176+
[st2] {{1 1} 1 0xc000014960}
177+
[st1 == st2] false
178+
[deepequal(st1,st2)] true
179+
===> 等価かどうかのチェック (2)
180+
===> 片方の構造体のデータを変化させる (自身のフィールドのみを変化)
181+
[st1] {{1 1} 1 0xc000014958}
182+
[st2] {{1 1} 2 0xc000014960}
183+
[st1 == st2] false
184+
[deepequal(st1,st2)] false
185+
===> 等価かどうかのチェック (3)
186+
===> 片方の構造体のデータを変化させる (組み込み構造体側の公開フィールド値を変化)
187+
[st1] {{1 1} 1 0xc000014958}
188+
[st2] {{2 1} 1 0xc000014960}
189+
[st1 == st2] false
190+
[deepequal(st1,st2)] false
191+
===> 等価かどうかのチェック (4)
192+
===> 片方の構造体のデータを変化させる (組み込み構造体側の非公開フィールド値を変化)
193+
[st1] {{1 1} 1 0xc000014958}
194+
[st2] {{1 2} 1 0xc000014960}
195+
[st1 == st2] false
196+
[deepequal(st1,st2)] false
197+
===> 等価かどうかのチェック (5)
198+
===> 片方の構造体がポインタで保持している先のデータのフィールドを変化させる
199+
[st1] {{1 1} 1 0xc000014958}
200+
[st2] {{1 1} 1 0xc000014960}
201+
[st1 == st2] false
202+
[deepequal(st1,st2)] false
203+
===> 等価かどうかのチェック (6)
204+
===> 片方の構造体がポインタで保持しているデータ自体を変更
205+
===> しかし、データのフィールド値は同じ
206+
[st1] {{1 1} 1 0xc000014958}
207+
[st2] {{1 1} 1 0xc0000149e0}
208+
[st1 == st2] false
209+
[deepequal(st1,st2)] true
210+
211+
212+
[Elapsed] 170.21µs
213+
*/
214+
160215
}

examples/basic/structs/struct_empty_struct.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,27 @@ func EmptyStruct() error {
111111
log.Println("main goroutine wait done")
112112

113113
return nil
114+
115+
/*
116+
$ task
117+
task: [build] go build .
118+
task: [run] ./try-golang -onetime
119+
120+
ENTER EXAMPLE NAME: struct_empty_struct
121+
122+
[Name] "struct_empty_struct"
123+
EmptyStruct[0] EmptyInterface[16]
124+
v1[0xe7a6c0] v2[0xe7a6c0] addr_equal? [true]
125+
v3[0xe7a6c0(structs.es1)] v4[0xe7a6c0(structs.es2)]
126+
v5[0xe7a6c0(structs.es3)] v6[0xe7a6c0(structs.es4)]
127+
hello world
128+
main goroutine wait start
129+
==> gorouine begin
130+
==> gorouine end
131+
main goroutine wait done
132+
133+
134+
[Elapsed] 2.001088491s
135+
*/
136+
114137
}

examples/basic/structs/struct_memory_padding.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ func MemoryPadding() error {
2222
)
2323

2424
output.Stdoutf("[st4bytes]", "%d byte(s)\n", unsafe.Sizeof(st4bytes))
25+
output.Stdoutl("", st4bytes.Layout())
2526
output.StdoutHr()
2627

2728
output.Stdoutf("[st8bytes]", "%d byte(s)\n", unsafe.Sizeof(st8bytes))
29+
output.Stdoutl("", st8bytes.Layout())
2830
output.StdoutHr()
2931

3032
output.Stdoutf("[Padding 発生]", "%d byte(s)\n", unsafe.Sizeof(notGood))
@@ -35,4 +37,49 @@ func MemoryPadding() error {
3537
output.Stdoutl("", good.Layout())
3638

3739
return nil
40+
41+
/*
42+
$ task
43+
task: [build] go build .
44+
task: [run] ./try-golang -onetime
45+
46+
ENTER EXAMPLE NAME: struct_memory_padding
47+
48+
[Name] "struct_memory_padding"
49+
[st4bytes] 4 byte(s)
50+
51+
| Flag | | Value |
52+
--------------------------------------
53+
| bool (1) | padding (2) | int16 (2) |
54+
| 4 |
55+
56+
--------------------------------------------------
57+
[st8bytes] 8 byte(s)
58+
59+
| Flag | | Value |
60+
--------------------------------------
61+
| bool (1) | padding (3) | int32 (4) |
62+
| 4 | 4 |
63+
64+
--------------------------------------------------
65+
[Padding 発生] 12 byte(s)
66+
67+
| Flag1 | | ShortVal | Flag2 | | FloatVal |
68+
-----------------------------------------------------------------------------
69+
| bool (1) | padding (1) | int16 (2) | bool (1) | padding (3) | float32 (4) |
70+
| 4 | 4 | 4 |
71+
72+
--------------------------------------------------
73+
[Padding なし] 8 byte(s)
74+
75+
| FloatVal | ShortVal | Flag1 | Flag2 |
76+
-------------------------------------------------
77+
| float32 (4) | int16 (2) | bool (1) | bool (1) |
78+
| 4 | 4 |
79+
80+
81+
82+
[Elapsed] 96.28µs
83+
*/
84+
3885
}

0 commit comments

Comments
 (0)