-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e30fe30
commit fa9b293
Showing
9 changed files
with
146 additions
and
4 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
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,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Integer int | ||
|
||
func (i *Integer) Add(x int) { // receiver is val, not ptr | ||
println(int(*i) + x) | ||
} | ||
|
||
func main() { | ||
a := new(Integer) | ||
a.Add(4) | ||
|
||
fmt.Println(*a) | ||
} | ||
|
||
// Output: | ||
// 4 | ||
// 0 |
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 main | ||
|
||
import "fmt" | ||
|
||
type Integer int | ||
|
||
func (i **Integer) Add(x int) { | ||
**i += Integer(x) // Dereference twice to get to the actual Integer value and modify it | ||
} | ||
|
||
func main() { | ||
a := new(Integer) // a is a pointer to Integer | ||
b := &a // b is a pointer to a pointer to Integer | ||
|
||
// Since Add is defined on **Integer, you need to pass b | ||
b.Add(4) // Adds 4 to the value **b points to | ||
|
||
fmt.Println(**b) // Should print 4, as **b is the same as *a | ||
} | ||
|
||
// Error: | ||
// main/debug/type37d.gno:7: invalid receiver type **main.Integer (pointer type) |
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 main | ||
|
||
import "fmt" | ||
|
||
type IntArray []int | ||
type Arr *IntArray | ||
|
||
func print(arr Arr) { // receiver is val, not ptr | ||
println(arr) | ||
} | ||
|
||
func main() { | ||
a := new(IntArray) | ||
print(a) | ||
|
||
fmt.Println(*a) | ||
} | ||
|
||
// Output: | ||
// &(nil main.IntArray) | ||
// [] |
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,26 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type IntArray []int | ||
type Arr *IntArray | ||
|
||
func add(arr Arr) { // receiver is val, not ptr | ||
*arr = append(*arr, 1) | ||
} | ||
|
||
func main() { | ||
a := new(IntArray) | ||
add(a) | ||
|
||
fmt.Println(a) | ||
fmt.Println(*a) | ||
fmt.Println(len(*a)) | ||
fmt.Println((*a)[0]) | ||
} | ||
|
||
// Output: | ||
// &[1] | ||
// [1] | ||
// 1 | ||
// 1 |
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,22 @@ | ||
package main | ||
|
||
type foo interface { | ||
say() | ||
} | ||
|
||
func (f foo) echo() int { | ||
return 1 | ||
} | ||
|
||
type Bar struct{} | ||
|
||
func (b *Bar) say() {} | ||
|
||
func main() { | ||
var f foo | ||
f = &Bar{} | ||
println(f.echo()) | ||
} | ||
|
||
// Error: | ||
// main/debug/type39.gno:7: invalid receiver type main.foo (interface type) |
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 @@ | ||
package main | ||
|
||
type foo interface { | ||
say() | ||
} | ||
|
||
type FF foo | ||
|
||
func (f FF) echo() int { | ||
return 1 | ||
} | ||
|
||
type Bar struct{} | ||
|
||
func (b *Bar) say() {} | ||
|
||
func main() { | ||
var f foo | ||
f = &Bar{} | ||
println(f.echo()) | ||
} | ||
|
||
// Error: | ||
// main/debug/type39a.gno:9: invalid receiver type main.FF (interface type) |