-
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
beb48e7
commit 217dfc3
Showing
34 changed files
with
916 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,31 @@ | ||
gnoland start | ||
|
||
# add contract | ||
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/ptrmap -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/demo/ptrmap -func AddToMap -args 5 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/demo/ptrmap -func GetFromMap -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout '(5 int)' | ||
stdout OK! | ||
|
||
-- gno.mod -- | ||
module gno.land/r/demo/ptrmap | ||
|
||
-- realm.gno -- | ||
package ptrmap | ||
|
||
var ( | ||
m = map[*int]int{} | ||
i = new(int) | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
func GetFromMap() int { | ||
return m[i] | ||
} |
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,35 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]int{} | ||
i = new(int) | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
func GetFromMap() int { | ||
return m[i] | ||
} | ||
|
||
func init() { | ||
*i = 1 | ||
AddToMap(5) | ||
} | ||
|
||
// ----above is initialized and persisted before main is executed. | ||
|
||
func main() { | ||
r := GetFromMap() | ||
println(r == 5) | ||
|
||
*i = 2 // this changes TV, also Base of a pointer value | ||
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
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,27 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[int]int{} | ||
i = 0 | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
func GetFromMap() int { | ||
return m[i] | ||
} | ||
|
||
func init() { | ||
AddToMap(5) | ||
} | ||
|
||
func main() { | ||
r := GetFromMap() | ||
println(r == 5) | ||
} | ||
|
||
// Output: | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
type Foo struct { | ||
name string | ||
} | ||
|
||
var root *Foo | ||
|
||
var f = &Foo{name: "a"} | ||
var f2 = &Foo{name: "a2"} | ||
|
||
func init() { | ||
root = f // origin of root is set as f's | ||
} | ||
|
||
func main() { | ||
f.name = "b" | ||
println(root == f) | ||
|
||
root = f2 // root has new origin(and pointer value) | ||
println(root == f2) | ||
} | ||
|
||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]string{} | ||
a, b = 1, 2 | ||
arr = [2]*int{&a, &b} | ||
) | ||
|
||
func init() { | ||
m[arr[0]] = "first key" | ||
} | ||
|
||
func main() { | ||
println(m[arr[0]]) // Output: first key | ||
println(m[arr[1]] == "") | ||
} | ||
|
||
// Output: | ||
// first key | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]string{} | ||
a, b = 1, 2 | ||
S = []*int{&a, &b} // slice | ||
index = 0 | ||
) | ||
|
||
func init() { | ||
m[S[index]] = "first key" | ||
} | ||
|
||
func main() { | ||
println(m[S[index]]) // Output: first key | ||
println(m[S[1]] == "") | ||
} | ||
|
||
// Output: | ||
// first key | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]string{} | ||
a, b = 1, 2 | ||
S = []*int{&a, &b} | ||
) | ||
|
||
func init() { | ||
m[S[0]] = "first key" | ||
} | ||
|
||
func main() { | ||
println(m[S[0]]) // Output: first key | ||
println(m[S[1]] == "") | ||
} | ||
|
||
// Output: | ||
// first key | ||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
type MyStruct struct { | ||
Index int | ||
} | ||
|
||
var m = make(map[*int]string) | ||
var a, b = 1, 2 | ||
var s = []*int{&a, &b} | ||
var myStruct = MyStruct{Index: 0} | ||
|
||
func init() { | ||
m[s[myStruct.Index]] = "a" | ||
} | ||
|
||
func main() { | ||
println(m[s[myStruct.Index]]) | ||
} | ||
|
||
// Output: | ||
// a |
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,35 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]int{} | ||
i = new(int) | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
func GetFromMap() int { | ||
return m[i] | ||
} | ||
|
||
func init() { | ||
*i = 1 | ||
AddToMap(5) | ||
} | ||
|
||
// ----above is initialized and persisted before main is executed. | ||
|
||
func main() { | ||
r := GetFromMap() | ||
println(r == 5) | ||
|
||
*i = 2 // this changes TV, also Base of a pointer value | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// PKGPATH: gno.land/r/ptr_map | ||
package ptr_map | ||
|
||
var ( | ||
m = map[*int]int{} | ||
i = new(int) | ||
) | ||
|
||
func AddToMap(value int) { | ||
m[i] = value | ||
} | ||
|
||
func GetFromMap() int { | ||
j := *i | ||
return m[&j] | ||
} | ||
|
||
func init() { | ||
*i = 1 | ||
AddToMap(5) | ||
} | ||
|
||
func main() { | ||
r := GetFromMap() | ||
println(r == 5) | ||
|
||
*i = 2 | ||
r = GetFromMap() | ||
println(r == 5) | ||
} | ||
|
||
// Output: | ||
// false | ||
// false |
Oops, something went wrong.