On Wednesday, August 23, 2017 at 8:49:39 AM UTC-4, Alkesh Ghorpade wrote:
>
> Hi All,
>
> I am new to Go and trying to figure out difference between const vs var
> variables memory usage and initialization.
>
> I have following go code
> // way 1
> var a = []string{"abc", "def"}
>
> // way 2
> const (
> a = "abc"
> b = "def"
> )
>
> var a1 = []string{a, b}
>
> my variables a and a1 are defined in a file and not inside any function.
> Is there any difference in both ways. When I call variable a in any
> function does it creates a copy everytime it is called? Can you please tell
> me what happens when a or a1 is called and how they are referenced in
> memory when called.
>
a and a1 have no differences. For this specified example, defining the
consts is a non-sense.
when a and a1 are used elsewhere, their direct parts will be copied but
their underlying parts will not be copied.
copying their direct parts is very cheap operation.
http://www.tapirgames.com/blog/golang-underlying-values
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.