the relation between add key/value and mapassign method in go1.10.3











up vote
2
down vote

favorite
1












I am reading the source code of map in go1.10.3.It seemed there exist corresponding method about operation such as:



 makemap(t *maptype, hint int, h *hmap) *hmap ==> m = make(map[xx]yy)
mapaccess1(t *maptype, h *hmap, key unsafe.Pointer)==> m['key']


but I cant find the correspond method for the operation which add key/value as below:



 m['xx']='yy'


there exist a method called mapassign which has some similarity with this
operation.



 mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer


this will add a new key to the map, but as we can see, the input arguments has no value. And another question is when it has already this key, it maybe update this key.



        if !alg.equal(key, k) {
continue
}
// already have a mapping for key. Update it.
if t.needkeyupdate {//why??
typedmemmove(t.key, k, key)
}


since the two key is equal, why should update it?



summary:

1. the relation between add key/value operation and method mapassign?

2. why it maybe need to update the key since the insert key and the key which has already exist is equal in mapassign method?










share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    I am reading the source code of map in go1.10.3.It seemed there exist corresponding method about operation such as:



     makemap(t *maptype, hint int, h *hmap) *hmap ==> m = make(map[xx]yy)
    mapaccess1(t *maptype, h *hmap, key unsafe.Pointer)==> m['key']


    but I cant find the correspond method for the operation which add key/value as below:



     m['xx']='yy'


    there exist a method called mapassign which has some similarity with this
    operation.



     mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer


    this will add a new key to the map, but as we can see, the input arguments has no value. And another question is when it has already this key, it maybe update this key.



            if !alg.equal(key, k) {
    continue
    }
    // already have a mapping for key. Update it.
    if t.needkeyupdate {//why??
    typedmemmove(t.key, k, key)
    }


    since the two key is equal, why should update it?



    summary:

    1. the relation between add key/value operation and method mapassign?

    2. why it maybe need to update the key since the insert key and the key which has already exist is equal in mapassign method?










    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I am reading the source code of map in go1.10.3.It seemed there exist corresponding method about operation such as:



       makemap(t *maptype, hint int, h *hmap) *hmap ==> m = make(map[xx]yy)
      mapaccess1(t *maptype, h *hmap, key unsafe.Pointer)==> m['key']


      but I cant find the correspond method for the operation which add key/value as below:



       m['xx']='yy'


      there exist a method called mapassign which has some similarity with this
      operation.



       mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer


      this will add a new key to the map, but as we can see, the input arguments has no value. And another question is when it has already this key, it maybe update this key.



              if !alg.equal(key, k) {
      continue
      }
      // already have a mapping for key. Update it.
      if t.needkeyupdate {//why??
      typedmemmove(t.key, k, key)
      }


      since the two key is equal, why should update it?



      summary:

      1. the relation between add key/value operation and method mapassign?

      2. why it maybe need to update the key since the insert key and the key which has already exist is equal in mapassign method?










      share|improve this question













      I am reading the source code of map in go1.10.3.It seemed there exist corresponding method about operation such as:



       makemap(t *maptype, hint int, h *hmap) *hmap ==> m = make(map[xx]yy)
      mapaccess1(t *maptype, h *hmap, key unsafe.Pointer)==> m['key']


      but I cant find the correspond method for the operation which add key/value as below:



       m['xx']='yy'


      there exist a method called mapassign which has some similarity with this
      operation.



       mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer


      this will add a new key to the map, but as we can see, the input arguments has no value. And another question is when it has already this key, it maybe update this key.



              if !alg.equal(key, k) {
      continue
      }
      // already have a mapping for key. Update it.
      if t.needkeyupdate {//why??
      typedmemmove(t.key, k, key)
      }


      since the two key is equal, why should update it?



      summary:

      1. the relation between add key/value operation and method mapassign?

      2. why it maybe need to update the key since the insert key and the key which has already exist is equal in mapassign method?







      dictionary go






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 at 10:03









      nail fei

      1,159518




      1,159518
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted











          1. In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.


          2. The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.







          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%2f53139622%2fthe-relation-between-add-key-value-and-mapassign-method-in-go1-10-3%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted











            1. In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.


            2. The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.







            share|improve this answer



























              up vote
              3
              down vote



              accepted











              1. In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.


              2. The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.







              share|improve this answer

























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted







                1. In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.


                2. The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.







                share|improve this answer















                1. In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.


                2. The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 days ago

























                answered 2 days ago









                ThunderCat

                47.4k45873




                47.4k45873






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139622%2fthe-relation-between-add-key-value-and-mapassign-method-in-go1-10-3%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini