Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aee4333

Browse files
Go Methods
1 parent 61b94de commit aee4333

File tree

8 files changed

+226
-0
lines changed
  • 13-methods
    • 01-method-example
    • 02-methods-are-functions
    • 03-same-method-names-on-different-types
    • 04-methods-with-pointer-receivers
    • 05-functions-with-pointer-arguments
    • 06-method-with-pointer-receiver-vs-function-with-pointer-argument
    • 07-method-with-value-receiver-vs-function-with-value-argument
    • 08-methods-on-non-struct-types

8 files changed

+226
-0
lines changed

‎13-methods/01-method-example/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Struct type - `Point`
8+
type Point struct {
9+
X, Y float64
10+
}
11+
12+
// Method with receiver `Point`
13+
func (p Point) IsAbove(y float64) bool {
14+
return p.Y > y
15+
}
16+
17+
func main() {
18+
p := Point{2.0, 4.0}
19+
20+
fmt.Println("Point : ", p)
21+
22+
fmt.Println("Is Point p located above the line y = 1.0 ? : ", p.IsAbove(1))
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Struct type - `Point`
8+
type Point struct {
9+
X, Y float64
10+
}
11+
12+
func IsAboveFunc(p Point, y float64) bool {
13+
return p.Y > y
14+
}
15+
16+
/*
17+
Compare the above function with the corresponding method -
18+
func (p Point) IsAbove(y float64) bool {
19+
return p.Y > y
20+
}
21+
*/
22+
23+
func main() {
24+
p := Point{2.5, -3.0}
25+
26+
fmt.Println("Point : ", p)
27+
28+
fmt.Println("Is Point p located above the line y = 1.0 ? : ", IsAboveFunc(p, 1))
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
type ArithmeticProgression struct {
9+
A, D float64
10+
}
11+
12+
// Method with receiver `ArithmeticProgression`
13+
func (ap ArithmeticProgression) NthTerm(n int) float64 {
14+
return ap.A + float64(n-1)*ap.D
15+
}
16+
17+
type GeometricProgression struct {
18+
A, R float64
19+
}
20+
21+
// Method with receiver `GeometricProgression`
22+
func (gp GeometricProgression) NthTerm(n int) float64 {
23+
return gp.A * math.Pow(gp.R, float64(n-1))
24+
}
25+
26+
func main() {
27+
ap := ArithmeticProgression{1, 2} // AP: 1 3 5 7 9 ...
28+
gp := GeometricProgression{1, 2} // GP: 1 2 4 8 16 ...
29+
30+
fmt.Println("5th Term of the Arithmetic series = ", ap.NthTerm(5))
31+
fmt.Println("5th Term of the Geometric series = ", gp.NthTerm(5))
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Point struct {
8+
X, Y float64
9+
}
10+
11+
/*
12+
Translates the current Point, at location (X,Y), by dx along the x axis and dy along the y axis
13+
so that it now represents the point (X+dx,Y+dy).
14+
*/
15+
func (p *Point) Translate(dx, dy float64) {
16+
p.X = p.X + dx
17+
p.Y = p.Y + dy
18+
}
19+
20+
func main() {
21+
p := Point{3, 4}
22+
fmt.Println("Point p = ", p)
23+
24+
p.Translate(7, 9)
25+
fmt.Println("After Translate, p = ", p)
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Point struct {
8+
X, Y float64
9+
}
10+
11+
/*
12+
Translates the current Point, at location (X,Y), by dx along the x axis and dy along the y axis
13+
so that it now represents the point (X+dx,Y+dy).
14+
*/
15+
func TranslateFunc(p *Point, dx, dy float64) {
16+
p.X = p.X + dx
17+
p.Y = p.Y + dy
18+
}
19+
20+
func main() {
21+
p := Point{3, 4}
22+
fmt.Println("Point p = ", p)
23+
24+
TranslateFunc(&p, 7, 9)
25+
fmt.Println("After Translate, p = ", p)
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Point struct {
8+
X, Y float64
9+
}
10+
11+
// Method with Pointer receiver
12+
func (p *Point) Translate(dx, dy float64) {
13+
p.X = p.X + dx
14+
p.Y = p.Y + dy
15+
}
16+
17+
// Function with Pointer argument
18+
func TranslateFunc(p *Point, dx, dy float64) {
19+
p.X = p.X + dx
20+
p.Y = p.Y + dy
21+
}
22+
23+
func main() {
24+
p := Point{3, 4}
25+
ptr := &p
26+
fmt.Println("Point p = ", p)
27+
28+
// Calling a Method with Pointer receiver
29+
p.Translate(2, 6) // Valid
30+
ptr.Translate(5, 10) // Valid
31+
32+
// Calling a Function with a Pointer argument
33+
TranslateFunc(ptr, 20, 30) // Valid
34+
// TranslateFunc(p, 20, 30) // Not Valid
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Struct type - `Point`
8+
type Point struct {
9+
X, Y float64
10+
}
11+
12+
func (p Point) IsAbove(y float64) bool {
13+
return p.Y > y
14+
}
15+
16+
func IsAboveFunc(p Point, y float64) bool {
17+
return p.Y > y
18+
}
19+
20+
func main() {
21+
p := Point{0, 4}
22+
ptr := &p
23+
24+
fmt.Println("Point p = ", p)
25+
26+
// Calling a Method with Value receiver
27+
p.IsAbove(1) // Valid
28+
ptr.IsAbove(1) // Valid
29+
30+
// Calling a Function with a Value argument
31+
IsAboveFunc(p, 1) // Valid
32+
// IsAboveFunc(ptr, 1) // Not Valid
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type MyString string
8+
9+
func (myStr MyString) reverse() string {
10+
s := string(myStr)
11+
runes := []rune(s)
12+
13+
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
14+
runes[i], runes[j] = runes[j], runes[i]
15+
}
16+
return string(runes)
17+
}
18+
19+
func main() {
20+
myStr := MyString("OLLEH")
21+
fmt.Println(myStr.reverse())
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /