-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #668 from devlights:add-zerovalue-example
Add-zerovalue-example
- Loading branch information
Showing
15 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# サンプルリスト | ||
|
||
このディレクトリには以下のサンプルがあります。 | ||
|
||
|file|example name|note| | ||
|----|------------|----| | ||
|int.go|zerovalues\_int|Goにおける int のゼロ値についてのサンプルです.| | ||
|float.go|zerovalues\_float|Goにおける float のゼロ値についてのサンプルです.| | ||
|bool.go|zerovalues\_bool|Goにおける bool のゼロ値についてのサンプルです.| | ||
|string.go|zerovalues\_string|Goにおける string のゼロ値についてのサンプルです.| | ||
|pointer.go|zerovalues\_pointer|Goにおける ポインタ のゼロ値についてのサンプルです.| | ||
|slice.go|zerovalues\_slice|Goにおける スライス のゼロ値についてのサンプルです.| | ||
|map.go|zerovalues\_map|Goにおける マップ のゼロ値についてのサンプルです.| | ||
|channel.go|zerovalues\_chan|Goにおける チャネル のゼロ値についてのサンプルです.| | ||
|func.go|zerovalues\_func|Goにおける 関数 のゼロ値についてのサンプルです.| | ||
|array.go|zerovalues\_array|Goにおける 配列 のゼロ値についてのサンプルです.| | ||
|struct.go|zerovalues\_struct|Goにおける 構造体 のゼロ値についてのサンプルです.| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Array は、Goにおける 配列 のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Array() error { | ||
// | ||
// 配列 の ゼロ値 は、その配列の基底型のゼロ値が指定要素数分設定されている状態. | ||
// | ||
var ( | ||
a [10]int | ||
) | ||
|
||
output.Stdoutf("[array zerovalue ]", "%v\n", a) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Bool は、Goにおける bool のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Bool() error { | ||
// | ||
// bool の ゼロ値 は false | ||
// | ||
var ( | ||
b bool | ||
) | ||
|
||
output.Stdoutf("[bool zerovalue]", "%t\n", b) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Channel は、Goにおける チャネル のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Channel() error { | ||
// | ||
// チャネル の ゼロ値 は nil | ||
// | ||
var ( | ||
ch chan int | ||
) | ||
|
||
output.Stdoutf("[chan zerovalue]", "%v\n", ch) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Package zerovalues -- Go言語の ゼロ値 についてのサンプルが配置されているパッケージです。 | ||
*/ | ||
package zerovalues |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package zerovalues | ||
|
||
import ( | ||
"github.com/devlights/try-golang/mapping" | ||
) | ||
|
||
type ( | ||
register struct{} | ||
) | ||
|
||
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。 | ||
func NewRegister() mapping.Register { | ||
return new(register) | ||
} | ||
|
||
// Regist -- 登録します. | ||
func (r *register) Regist(m mapping.ExampleMapping) { | ||
m["zerovalues_int"] = Int | ||
m["zerovalues_float"] = Float | ||
m["zerovalues_bool"] = Bool | ||
m["zerovalues_string"] = String | ||
m["zerovalues_pointer"] = Pointer | ||
m["zerovalues_slice"] = Slice | ||
m["zerovalues_map"] = Map | ||
m["zerovalues_chan"] = Channel | ||
m["zerovalues_func"] = Func | ||
m["zerovalues_array"] = Array | ||
m["zerovalues_struct"] = Struct | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Float は、Goにおける float のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Float() error { | ||
// | ||
// float (float32, float64) の ゼロ値 は 0.0 | ||
// | ||
var ( | ||
f float64 | ||
) | ||
|
||
output.Stdoutf("[float zerovalue]", "%.1f\n", f) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Func は、Goにおける 関数 のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Func() error { | ||
// | ||
// 関数 の ゼロ値 は nil | ||
// | ||
var ( | ||
fn1 func() | ||
fn2 = func() {} | ||
) | ||
|
||
output.Stdoutf("[func zerovalue]", "%p:%p\n", fn1, fn2) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Int は、Goにおける int のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Int() error { | ||
// | ||
// int の ゼロ値 は 0 | ||
// | ||
var ( | ||
i int | ||
) | ||
|
||
output.Stdoutf("[int zerovalue]", "%d\n", i) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Map は、Goにおける マップ のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Map() error { | ||
// | ||
// マップ の ゼロ値 は nil | ||
// 注意点として マップ の場合、ゼロ値をprintfすると map[] と表示される. | ||
// アドレスを表示すると ゼロ値 の場合は 0x0 と表示される. | ||
// スライスとは違い ゼロ値 のマップにはキーを追加出来ない. | ||
// | ||
var ( | ||
m map[int]string | ||
) | ||
|
||
output.Stdoutf("[map zerovalue]", "%v\t%p\tNIL?=%t\n", m, m, m == nil) | ||
|
||
// 以下のようにエラーとなる | ||
// m[100] = "apple" | ||
// >> panic: assignment to entry in nil map | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Pointer は、Goにおける ポインタ のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Pointer() error { | ||
// | ||
// ポインタ の ゼロ値 は nil | ||
// | ||
var ( | ||
p *int | ||
) | ||
|
||
output.Stdoutf("[pointer zerovalue]", "%v\n", p) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Slice は、Goにおける スライス のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Slice() error { | ||
// | ||
// スライス の ゼロ値 は nil | ||
// 注意点として スライス の場合、ゼロ値をprintfすると [] と表示される. | ||
// アドレスを表示すると ゼロ値 の場合は 0x0 と表示される. | ||
// また、append() には ゼロ値のスライス を渡すことが可能な点に注意. | ||
// | ||
var ( | ||
s []int | ||
) | ||
|
||
output.Stdoutf("[slice zerovalue]", "%v\t%p\tNIL?=%t\n", s, s, s == nil) | ||
|
||
s = append(s, 100) | ||
output.Stdoutf("[slice after append]", "%v\t%p\tNIL?=%t\n", s, s, s == nil) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// String は、Goにおける string のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func String() error { | ||
// | ||
// string の ゼロ値 は 空文字 ("") | ||
// | ||
var ( | ||
s string | ||
) | ||
|
||
output.Stdoutf("[string zerovalue]", "%q\n", s) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package zerovalues | ||
|
||
import "github.com/devlights/gomy/output" | ||
|
||
// Struct は、Goにおける 構造体 のゼロ値についてのサンプルです. | ||
// | ||
// # REFERENCES | ||
// - https://go.dev/tour/basics/12 | ||
// - https://brain2life.hashnode.dev/default-zero-values-in-go | ||
func Struct() error { | ||
// | ||
// 構造体 の ゼロ値 は、フィールドの型毎にゼロ値が設定された状態. | ||
// | ||
type ( | ||
_st struct { | ||
i int | ||
b bool | ||
} | ||
) | ||
|
||
var ( | ||
st _st | ||
) | ||
|
||
output.Stdoutf("[struct zerovalue]", "%+v\n", st) | ||
|
||
st.i = 100 | ||
st.b = true | ||
output.Stdoutf("[struct assign values]", "%+v\n", st) | ||
|
||
return nil | ||
} |