#is this a slice or an array ?

54 messages · Page 1 of 1 (latest)

pure fiber
#

It's a slice

#

With a size of 2 and a cap of 2

#

Everything that is [] is a slice

prisma cedar
#

even with a size ? it is still a slice ?

pure fiber
#

Yes

#

All slices have a size

prisma cedar
#

ok i understand

#

var tst [1]string = [1]string{"hello"}

#

even this ?

pure fiber
#

That's an array

prisma cedar
#

it is a slice ?

#

hmmm

pure fiber
#

[x] - array
[] - slice

prisma cedar
#

but

#

hmmmmmm confusing

#

soooo

#

when i use the make function

#

to make a slice with a length

#

it is like i already calculated the length right ?

pure fiber
#

I don't understand

prisma cedar
#

when i do this

#

make([]string, 2)

#

2 is the length right ?

pure fiber
#

Yes

prisma cedar
#

so

#

when i do this

#

tst := []string{"hello"}

#

and i use the len() function

#

the len function will return the length right ?

pure fiber
#

Yes

prisma cedar
#

so

#

the length in make the same as using len() with any datatype

#

????

pure fiber
#

I think so? Maybe there is an exception but I can't think of one right now

prisma cedar
#

i seeeee

pure fiber
#

What's to remember is that arrays are static and can't be expanded, while slices can be appended to

prisma cedar
#

yes of course

#

because a slice doesn't have a size

covert belfry
#

@prisma cedar

  • slices: runtime length and cap stored in the slice header
  • arrays: compile-time length and cap stored in the type definition
prisma cedar
#

wait wait wait

covert belfry
#

a slice have a size, everything has a size

prisma cedar
#

slice already have the length why do we need capacity???????????

covert belfry
#

the length of the slice is set at runtime and is not part of the type

covert belfry
prisma cedar
#

what is O(1) ????

covert belfry
#

wikipedia, google, youtube, chatgpt, ... are all able to explain you this

prisma cedar
#

fine

prisma cedar
covert belfry
covert belfry
prisma cedar
glad cloud
marble karma
wide rover
#

The capacity of a slice is the size of the underlying array.
The length of a slice is how much of the underlying array you're using.