Skip to content

Commit 27a7047

Browse files
committed
Add scope_common_mistake{1-3}.go
1 parent 1b87136 commit 27a7047

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

examples/basic/scope/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
|file|example name|note|
66
|----|------------|----|
77
|scope\_basic.go|scope\_basic|スコープについての基本的な事項についてのサンプルです.|
8+
|scope\_common\_mistake1.go|scope\_common\_mistake1|変数宣言のスコープによるよくやる間違いについてのサンプルです.|
9+
|scope\_common\_mistake2.go|scope\_common\_mistake2|CommonMistake1の間違い修正パターン (1)|
10+
|scope\_common\_mistake3.go|scope\_common\_mistake3|CommonMistake1の間違い修正パターン (2)|
811

examples/basic/scope/examples.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ func NewRegister() mappings.Register {
1616
// Regist -- 登録します.
1717
func (r *register) Regist(m mappings.ExampleMapping) {
1818
m["scope_basic"] = Basic
19+
m["scope_common_mistake1"] = CommonMistake1
20+
m["scope_common_mistake2"] = CommonMistake2
21+
m["scope_common_mistake3"] = CommonMistake3
1922
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package scope
2+
3+
import (
4+
"os"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
var (
10+
// 現在の作業ディレクトリ. CommonMistake関数で使う前に初期化されている想定
11+
_cwd1 string
12+
)
13+
14+
func loadcwd() error {
15+
// 一見、ちゃんと os.Getwd() の結果を パッケージ変数 _cwd1 に設定できているように見えるが・・
16+
_cwd1, err := os.Getwd()
17+
if err != nil {
18+
return err
19+
}
20+
21+
// ここでの結果はちゃんと表示される
22+
output.Stdoutl("[loadcwd]", _cwd1)
23+
24+
return nil
25+
}
26+
27+
// CommonMistake1 -- 変数宣言のスコープによるよくやる間違いについてのサンプルです.
28+
func CommonMistake1() error {
29+
if err := loadcwd(); err != nil {
30+
return err
31+
}
32+
33+
output.Stdoutl("[main]", _cwd1)
34+
35+
// -------------------------------------------------------------------------
36+
// 実行すると、上の _cwd1 の値は空文字で出力される.
37+
// 理由は、loadcwd() で設定している _cwd1 は、省略変数宣言 := を使っているため
38+
// ローカル変数 _cwd1 が新たに生成されてしまったため。
39+
//
40+
// loadcwd() 内で、最後にログ出力変わりに ローカル変数 _cwd1 の値を出力する
41+
// ようにしているため、一見ちゃんと設定できているように見えるし
42+
// ログ出力するために変数を使用しているため、コンパイルエラーにもならない
43+
// (これがログ出力部分がなかったら、ローカル変数を使用していないという
44+
// コンパイルエラーとなるため、そこで気づける可能性がある。)
45+
// -------------------------------------------------------------------------
46+
47+
return nil
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package scope
2+
3+
import (
4+
"os"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
var (
10+
_cwd2 string
11+
)
12+
13+
func loadcwd2() error {
14+
// ローカルスコープで _cwd2 が生成されるのを防ぐために
15+
// err を var宣言 で先に宣言し、:= を使わないようにする
16+
var err error
17+
18+
_cwd2, err = os.Getwd()
19+
if err != nil {
20+
return err
21+
}
22+
23+
output.Stdoutl("[loadcwd]", _cwd2)
24+
25+
return nil
26+
}
27+
28+
// CommonMistake2 -- CommonMistake1の間違い修正パターン (1)
29+
func CommonMistake2() error {
30+
if err := loadcwd2(); err != nil {
31+
return err
32+
}
33+
34+
output.Stdoutl("[main]", _cwd2)
35+
36+
return nil
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package scope
2+
3+
import (
4+
"os"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
type (
10+
_pkginfo struct {
11+
cwd string
12+
}
13+
)
14+
15+
var (
16+
pkginfo _pkginfo
17+
)
18+
19+
func loadcwd3() error {
20+
cwd, err := os.Getwd()
21+
if err != nil {
22+
return err
23+
}
24+
25+
pkginfo.cwd = cwd
26+
output.Stdoutl("[loadcwd]", pkginfo.cwd)
27+
28+
return nil
29+
}
30+
31+
// CommonMistake3 -- CommonMistake1の間違い修正パターン (2)
32+
func CommonMistake3() error {
33+
if err := loadcwd3(); err != nil {
34+
return err
35+
}
36+
37+
output.Stdoutl("[main]", pkginfo.cwd)
38+
39+
return nil
40+
}

0 commit comments

Comments
 (0)