-
Notifications
You must be signed in to change notification settings - Fork 389
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
7ae8646
commit 304e8e3
Showing
10 changed files
with
33 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
type Foo struct { | ||
name string | ||
} | ||
var ( | ||
m = map[int]int{} | ||
i = 0 | ||
) | ||
|
||
var root *Foo | ||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
var f = &Foo{name: "a"} | ||
var f2 = &Foo{name: "a2"} | ||
func GetFromMap() int { | ||
return m[i] | ||
} | ||
|
||
func init() { | ||
root = f // origin of root is set as f's | ||
AddToMap(5) | ||
} | ||
|
||
func main() { | ||
f.name = "b" | ||
println(root == f) | ||
|
||
root = f2 // root has new origin(and pointer value) | ||
println(root == f2) | ||
r := GetFromMap() | ||
println(r == 5) | ||
} | ||
|
||
// Output: | ||
// true | ||
// true |
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 |
---|---|---|
@@ -1,34 +1,23 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]int{} | ||
i = new(int) | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
type MyStruct struct { | ||
Key *int | ||
} | ||
|
||
func GetFromMap() int { | ||
j := *i | ||
return m[&j] | ||
} | ||
var ( | ||
m = map[*int]string{} | ||
i1 = MyStruct{Key: new(int)} | ||
) | ||
|
||
func init() { | ||
*i = 1 | ||
AddToMap(5) | ||
*i1.Key = 1 // Set the value of the pointer | ||
m[i1.Key] = "first key" | ||
} | ||
|
||
func main() { | ||
r := GetFromMap() | ||
println(r == 5) | ||
|
||
*i = 2 | ||
r = GetFromMap() | ||
println(r == 5) | ||
println(m[i1.Key]) // Output: first key | ||
} | ||
|
||
// Output: | ||
// false | ||
// false | ||
// first key |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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