Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/examples/basic/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/devlights/try-golang/internal/examples/basic/logging"
"github.com/devlights/try-golang/internal/examples/basic/maps"
"github.com/devlights/try-golang/internal/examples/basic/maths"
"github.com/devlights/try-golang/internal/examples/basic/methods"
"github.com/devlights/try-golang/internal/examples/basic/network"
"github.com/devlights/try-golang/internal/examples/basic/runtimes"
"github.com/devlights/try-golang/internal/examples/basic/scope"
Expand Down Expand Up @@ -87,6 +88,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
logging.NewRegister().Regist(m)
maps.NewRegister().Regist(m)
maths.NewRegister().Regist(m)
methods.NewRegister().Regist(m)
network.NewRegister().Regist(m)
runtimes.NewRegister().Regist(m)
scope.NewRegister().Regist(m)
Expand Down
7 changes: 7 additions & 0 deletions internal/examples/basic/methods/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# サンプルリスト

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

|file|example name|note|
|----|------------|----|
|pointer\_or\_not.go|methods\_pointer\_or\_not|メソッドのレシーバーをポインタで宣言するかしないかの違いについてのサンプルです.|
4 changes: 4 additions & 0 deletions internal/examples/basic/methods/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package methods -- メソッドについてのサンプルが配置されているパッケージです。
*/
package methods
19 changes: 19 additions & 0 deletions internal/examples/basic/methods/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package methods

import (
"github.com/devlights/try-golang/pkg/mappings"
)

type (
register struct{}
)

// NewRegister -- このパッケージ用のサンプルを登録する mappings.Register を生成します。
func NewRegister() mappings.Register {
return new(register)
}

// Regist -- 登録します.
func (r *register) Regist(m mappings.ExampleMapping) {
m["methods_pointer_or_not"] = PointerOrNot
}
39 changes: 39 additions & 0 deletions internal/examples/basic/methods/pointer_or_not.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package methods

import "github.com/devlights/gomy/output"

type (
_nonPointerReceiver struct {
val int
}

_pointerReceiver struct {
val int
}
)

func (me _nonPointerReceiver) update(val int) {
me.val = val
}

func (me *_pointerReceiver) update(val int) {
me.val = val
}

// PointerOrNot は、メソッドのレシーバーをポインタで宣言するかしないかの違いについてのサンプルです。
func PointerOrNot() error {
var (
nonPointer = _nonPointerReceiver{}
pointer = _pointerReceiver{}
)

// これは内部の値が更新されない
nonPointer.update(100)
// これは内部の値が更新される
pointer.update(100)

output.Stdoutl("[non-pointer]", nonPointer.val)
output.Stdoutl("[pointer ]", pointer.val)

return nil
}