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











share|improve this question




















  • 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















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











share|improve this question




















  • 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













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











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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






    share|improve this answer



























      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






      share|improve this answer

























        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






        share|improve this answer














        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 at 0:51

























        answered Nov 14 at 0:45









        landru27

        750213




        750213






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?