GithubHelp home page GithubHelp logo

golang-101-hacks's Introduction

golang-101-hacks's People

Contributors

nanxiao avatar tevjef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

golang-101-hacks's Issues

Update explanation of slices as arguments

Hello,

Thank you for sharing your knowledge with the rest of the community!

I was looking at your explanation of passing slices as function argumens (https://github.com/NanXiao/golang-101-hacks/raw/master/posts/pass-slice-as-a-function-argument.md) and found that it may be nice to expand on it a bit:

This time, the addValue function doesn't take effect on the s slice in main function. That's because it just manipulate the copy of the s, not the "real" s.

From https://golang.org/doc/effective_go.html?#slices we know that what happens here is that the value s, containing a pointer to an underlying array, is copied when passed as an argument to addValue.
In order to distinguish the two values, let's call the copy of s that exists within addValue cs.

When we use append to update cs, it just so happens that the capacity of the underlying array is exceeded. A new array is allocated, the values from the old array is copied to the new array, and the pointer in cs is updated to point to the new array.
Since our changes to cs are not reflected in s, the pointer to the underlying array in s is not updated and therefore points to the old array. This is the effect you show in your example.

If the underlying array happens to have enough capacity to accommodate the appended data, append actually does append data to the underlying array that s points to. See an example here: https://play.golang.org/p/eLQ9doxuWze

We see that s "is not aware" that more data has been added to the underlying array. If we want to read all of the data of the underlying array, we have to read beyond the length (not capacity) of the array. This is because the length is stored as a value in s, which is updated in cs during append, but, for the same reasons as with the updated pointer to the underlying array above, this change is not reflected in s.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.