Skip to content

Commit 2ec3c7c

Browse files
authored
Merge pull request #402 from devlights/devlights/for-for-each-397
Add examples/basic/loops package
2 parents 2122339 + 07f9dcd commit 2ec3c7c

File tree

9 files changed

+163
-0
lines changed

9 files changed

+163
-0
lines changed

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/devlights/try-golang/examples/basic/internalpkg"
2828
"github.com/devlights/try-golang/examples/basic/literals"
2929
"github.com/devlights/try-golang/examples/basic/logging"
30+
"github.com/devlights/try-golang/examples/basic/loops"
3031
"github.com/devlights/try-golang/examples/basic/maps"
3132
"github.com/devlights/try-golang/examples/basic/maths"
3233
"github.com/devlights/try-golang/examples/basic/methods"
@@ -90,6 +91,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
9091
streams.NewRegister().Regist(m)
9192
literals.NewRegister().Regist(m)
9293
logging.NewRegister().Regist(m)
94+
loops.NewRegister().Regist(m)
9395
maps.NewRegister().Regist(m)
9496
maths.NewRegister().Regist(m)
9597
methods.NewRegister().Regist(m)

examples/basic/loops/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
|file|example name|note|
6+
|----|------------|----|
7+
|basic\_for\_loop.go|loops\_basic\_for\_loop|他の言語でも同じように存在する基本的な for-loop についてのサンプルです|
8+
|basic\_foreach.go|loops\_basic\_foreach|Go での foreach ループのサンプルです|
9+
|channel\_loop.go|loops\_channel\_loop|チャネルをループさせる場合のサンプルです|
10+
|map\_loop.go|loops\_map\_loop|map のループについてのサンプルです|
11+
|range\_loop.go|loops\_range\_loop|単純に指定回数ループするためのサンプルです|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package loops
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// BasicForLoop は、他の言語でも同じように存在する基本的な for-loop についてのサンプルです.
6+
func BasicForLoop() error {
7+
var (
8+
items = []string{
9+
"go",
10+
"java",
11+
"dotnet",
12+
"python",
13+
"flutter",
14+
}
15+
)
16+
17+
// 他の言語と同じように Go にも インデックス 付きの for-loop がある
18+
for i := 0; i < 5; i++ {
19+
output.Stdoutf("", "[%d] %s\n", i, items[i])
20+
}
21+
22+
return nil
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package loops
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// BasicForeach は、Go での foreach ループについてのサンプルです.
6+
func BasicForeach() error {
7+
var (
8+
items = []string{
9+
"go",
10+
"java",
11+
"dotnet",
12+
"python",
13+
"flutter",
14+
}
15+
)
16+
17+
for i, v := range items {
18+
output.Stdoutf("", "[%d] %s\n", i, v)
19+
}
20+
21+
return nil
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package loops
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// ChannelLoop は、チャネルをループさせる場合のサンプルです.
6+
func ChannelLoop() error {
7+
var (
8+
ch = make(chan string)
9+
quit = make(chan struct{})
10+
items = []string{
11+
"go",
12+
"java",
13+
"dotnet",
14+
"python",
15+
"flutter",
16+
}
17+
)
18+
19+
go func(ch chan<- string) {
20+
defer close(ch)
21+
22+
for _, v := range items {
23+
ch <- v
24+
}
25+
}(ch)
26+
27+
go func(quit chan<- struct{}, ch <-chan string) {
28+
defer close(quit)
29+
30+
// チャネルを foreach ループする場合, インデックスは付かない
31+
for v := range ch {
32+
output.Stdoutl("", v)
33+
}
34+
}(quit, ch)
35+
36+
<-quit
37+
38+
return nil
39+
}

examples/basic/loops/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package loops -- Go のループについてのサンプルが配置されています.
3+
*/
4+
package loops

examples/basic/loops/examples.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package loops
2+
3+
import "github.com/devlights/try-golang/mapping"
4+
5+
type (
6+
register struct{}
7+
)
8+
9+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
10+
func NewRegister() mapping.Register {
11+
return new(register)
12+
}
13+
14+
// Regist -- 登録します.
15+
func (r *register) Regist(m mapping.ExampleMapping) {
16+
m["loops_basic_for_loop"] = BasicForLoop
17+
m["loops_basic_foreach"] = BasicForeach
18+
m["loops_channel_loop"] = ChannelLoop
19+
m["loops_map_loop"] = MapLoop
20+
m["loops_range_loop"] = RangeLoop
21+
}

examples/basic/loops/map_loop.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package loops
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// MapLoop は、 map のループについてのサンプルです.
6+
func MapLoop() error {
7+
var (
8+
m = map[string]string{
9+
"go": "fmt.Println",
10+
"java": "System.out.println",
11+
"dotnet": "Console.WriteLine",
12+
"python": "print",
13+
}
14+
)
15+
16+
// map の ループ は、key, value の値が毎ターン取得できる
17+
for k, v := range m {
18+
output.Stdoutf("", "[%s] %s\n", k, v)
19+
}
20+
21+
return nil
22+
}

examples/basic/loops/range_loop.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package loops
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// RangeLoop は、単純に指定回数ループするためのサンプルです.
6+
func RangeLoop() error {
7+
// 単純に指定回数だけループしたい場合、[]struct{} を作って
8+
// ループさせるのが効率が良い. struct{} はメモリを消費しない.
9+
for range make([]struct{}, 3) {
10+
output.Stdoutl("", "hello")
11+
}
12+
13+
// インデックスが欲しい場合
14+
for i := range make([]struct{}, 3) {
15+
output.Stdoutf("", "[%d] hello\n", i)
16+
}
17+
18+
return nil
19+
}

0 commit comments

Comments
 (0)