-
Notifications
You must be signed in to change notification settings - Fork 19
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 #51 from tsingbx/method-values
Add doc for method values and expressions
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
# Method value | ||
|
||
// Method values allow you to bind a method to a specific object, and then call the method as an ordinary function with the object implied, in a kind of closure. For example: | ||
|
||
type Hello struct { | ||
} | ||
|
||
func (hello *Hello) Say() { | ||
println "Hello" | ||
} | ||
|
||
hello := &Hello{} | ||
|
||
hello.Say | ||
|
||
printf("%T", hello.Say) | ||
|
||
// The expression hello.Say creates a method value, binding the Say function to the specific variable hello, giving it a type of func(). So function values can be passed as function parameters |
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,25 @@ | ||
|
||
# Method expressions | ||
|
||
// Method expressions let you transform a method into a function with the receiver as its first argument. For example: | ||
|
||
type Point struct { | ||
x float64 | ||
y float64 | ||
} | ||
|
||
func (p Point) Add(another Point) Point { | ||
return Point{p.x+another.x, p.y+another.y} | ||
} | ||
|
||
func (p Point) String() string { | ||
return sprintf("x: %.1f, y: %.1f", p.x, p.y) | ||
} | ||
|
||
p := Point{20, 10} | ||
another := Point{30, 40} | ||
|
||
println p.Add(another) | ||
println Point.Add(p, another) | ||
|
||
// So if you defined a Point struct and a method func (p Point) Add(another Point), you could write Point.Add to get a func(p Point, another Point). |
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,24 @@ | ||
|
||
// But if the method has a pointer receiver, you need to write (*Type).Method in your method expression. For example: | ||
|
||
type Point struct { | ||
x float64 | ||
y float64 | ||
} | ||
|
||
func (p *Point)Add(another Point) { | ||
p.x += another.x | ||
p.y += another.y | ||
} | ||
|
||
func (p *Point) String() string { | ||
return sprintf("x: %.1f, y: %.1f", p.x, p.y) | ||
} | ||
|
||
p := &Point{20, 10} | ||
another := Point{30, 40} | ||
|
||
p.Add(another) | ||
(*Point).Add(p, another) | ||
|
||
println p |
This file was deleted.
Oops, something went wrong.