On Sat, Jan 25, 2020 at 9:15 AM burak serdar <bser...@computer.org> wrote:
>
> On Sat, Jan 25, 2020 at 9:55 AM Kaleemullah Software Engineer
> <kaleemullah...@gmail.com> wrote:
> >
> >
> > When i change the value U at 0 index , It changes the value of t and u at 
> > zero index why it is not changing the value of S. Please Explain I got 
> > stuck here.
> >
> >
> >
> >
> > package main
> >
> > import (
> > "fmt"
> > )
> >
> > func main() {
> >
> > var s []int
> > var t []int
> > s= make([]int,3)
>
> Here, you allocate a slice with cap=3,
>
> > s[0]=100
> > s[1]=200
> > s[2]=300
> > t=append(s,400)
>
> Here, you append an element to s, which has cap=3, so a new slice is
> allocated with larger cap, that becomes t. s still has cap=3 and it
> has its three elements. t has 4 elements,
>
> > fmt.Println(s,len(s),cap(s))
> > fmt.Println(t,len(t),cap(t))
> > var u []int
> > u=append(t,500)
>
> Here, you append an element to t, and apparently t has capacity, so it
> does not reallocate a new array. t and u are pointing to the same
> underlying array, with len(t)=4 and len(u)=5
>
> > fmt.Println(u,len(u),cap(u))
> > u[0]=9999
>
> This sets the first element of the underlying array for t and u.
>
> > fmt.Println("//////////////////////")
> >
> > fmt.Println(s,len(s),cap(s))
> > fmt.Println(t,len(t),cap(t))
> > fmt.Println(u,len(u),cap(u))
> > }



Please read https://blog.golang.org/slices.

Ian

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWp188utufLBq_xgSk_y_VH3xS5%3DzP8EOaXZOXDg5%3D%3DUQ%40mail.gmail.com.

Reply via email to