Do map of pointers is different with common way of using maps
up vote
-3
down vote
favorite
I want to create cache with map. As map doesn't allow reference to its value, so it's not possible to change values in called functions.
After some search, I found, it's possible with creating map of pointer (of struct). It Almost solve problem and can work like variable by reference
But as i found a few using of this method for map. I worry about using it to be safe.
Is anyone has experience of using map of pointer? and is it right way to use it?
package main
import "fmt"
type Cache struct {
name string
counter int
}
func incr(c Cache) {
c.counter += 1
}
func incrp(c *Cache) {
c.counter += 2
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
incr(c)
fmt.Println(c.name, c.counter) // James 10
mp := make(map[string]*Cache)
mp["james"] = &Cache{name: "James", counter: 10}
cp := mp["james"]
incrp(cp)
fmt.Println(cp.name, cp.counter) // James 12
}
edited: My text had some confusing words and sentences, that caused to misunderstanding, so i tried to fixed it
go
add a comment |
up vote
-3
down vote
favorite
I want to create cache with map. As map doesn't allow reference to its value, so it's not possible to change values in called functions.
After some search, I found, it's possible with creating map of pointer (of struct). It Almost solve problem and can work like variable by reference
But as i found a few using of this method for map. I worry about using it to be safe.
Is anyone has experience of using map of pointer? and is it right way to use it?
package main
import "fmt"
type Cache struct {
name string
counter int
}
func incr(c Cache) {
c.counter += 1
}
func incrp(c *Cache) {
c.counter += 2
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
incr(c)
fmt.Println(c.name, c.counter) // James 10
mp := make(map[string]*Cache)
mp["james"] = &Cache{name: "James", counter: 10}
cp := mp["james"]
incrp(cp)
fmt.Println(cp.name, cp.counter) // James 12
}
edited: My text had some confusing words and sentences, that caused to misunderstanding, so i tried to fixed it
go
2
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
1
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
2
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I want to create cache with map. As map doesn't allow reference to its value, so it's not possible to change values in called functions.
After some search, I found, it's possible with creating map of pointer (of struct). It Almost solve problem and can work like variable by reference
But as i found a few using of this method for map. I worry about using it to be safe.
Is anyone has experience of using map of pointer? and is it right way to use it?
package main
import "fmt"
type Cache struct {
name string
counter int
}
func incr(c Cache) {
c.counter += 1
}
func incrp(c *Cache) {
c.counter += 2
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
incr(c)
fmt.Println(c.name, c.counter) // James 10
mp := make(map[string]*Cache)
mp["james"] = &Cache{name: "James", counter: 10}
cp := mp["james"]
incrp(cp)
fmt.Println(cp.name, cp.counter) // James 12
}
edited: My text had some confusing words and sentences, that caused to misunderstanding, so i tried to fixed it
go
I want to create cache with map. As map doesn't allow reference to its value, so it's not possible to change values in called functions.
After some search, I found, it's possible with creating map of pointer (of struct). It Almost solve problem and can work like variable by reference
But as i found a few using of this method for map. I worry about using it to be safe.
Is anyone has experience of using map of pointer? and is it right way to use it?
package main
import "fmt"
type Cache struct {
name string
counter int
}
func incr(c Cache) {
c.counter += 1
}
func incrp(c *Cache) {
c.counter += 2
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
incr(c)
fmt.Println(c.name, c.counter) // James 10
mp := make(map[string]*Cache)
mp["james"] = &Cache{name: "James", counter: 10}
cp := mp["james"]
incrp(cp)
fmt.Println(cp.name, cp.counter) // James 12
}
edited: My text had some confusing words and sentences, that caused to misunderstanding, so i tried to fixed it
go
go
edited Nov 14 at 8:08
Flimzy
36.6k96496
36.6k96496
asked Nov 13 at 20:19
irmorteza
51311023
51311023
2
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
1
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
2
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11
add a comment |
2
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
1
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
2
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11
2
2
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
1
1
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
2
2
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can accomplish this and still have a map
of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can accomplish this and still have a map
of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4
add a comment |
up vote
1
down vote
You can accomplish this and still have a map
of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4
add a comment |
up vote
1
down vote
up vote
1
down vote
You can accomplish this and still have a map
of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4
You can accomplish this and still have a map
of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4
edited Nov 14 at 0:51
answered Nov 14 at 0:45
landru27
750213
750213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288885%2fdo-map-of-pointers-is-different-with-common-way-of-using-maps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
No one can answer that for your application with your data running at idea of "large scale". Just run it, measure it, and see if performance is acceptable, and if not, profile it to find out why.
– Adrian
Nov 13 at 20:22
I don't want the performance exactly. I want to know is it right way to using this @Adrian
– irmorteza
Nov 13 at 20:24
1
There is no "right way" as this depends on your workload and nobody can tell you. You have to measure yourself.
– Volker
Nov 13 at 20:27
2
Sure, using pointers is a fine way, and will almost certainly not be a performance problem for you. Just write it, measure, and see.
– Alex Guerra
Nov 13 at 20:28
thanks @AlexGuerra
– irmorteza
Nov 13 at 21:11