Please help me understand - let me know if I understand this correctly and, if you can, answer my questions. I’m 10 months new to Go and 2 years new to programming.
Slices, Maps, and Channels are reference types. This means they are variables storing an address pointing to some other data structure where the literal values of a primitive / built-in / basic type are stored.
For instance, a slice is “built on-top of” an underlying array. If you have …
xs := []int{2, 3, 7, 8}
fmt.Println(xs)
fmt.Println(len(xs))
fmt.Println(cap(xs))
xs = append(xs, 9)
fmt.Println(len(xs))
fmt.Println(cap(xs))
https://play.golang.org/p/WxVSs4drt1
… then xs is of type []int and that type has a “header” value which stores three things: the address pointing to the underlying array, the length of the slice, and the capacity of the underlying array.
QUESTION #1:
Since a slice is already a reference type, it seems as if I would never pass the value of a slice’s address. Is that correct? When I look at this documentation however …
https://cloud.google.com/appengine/docs/go/datastore/reference#Query.GetAll
… the requested arguments should be of this type:
*[]S or *[]*S or *[]P
also from docs: "... for struct type S or some non- interface, non-pointer type P ..."
QUESTION #2
For one project, I’m working with images.
When I look in the docs at the Image type from package image, I see this:
type Image interface {
// ColorModel returns the Image's color model.
ColorModel() color.Model
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
Bounds() Rectangle
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
At(x, y int) color.Color
}
In my code, I’m opening a bunch of images and storing them in a slice.
var images []image.Image
When I return a value of type []image.Image, the value I am passing is the “address to the underlying array where the images are stored, the length of the slice, and the capacity of the slice” correct? Is the actual image data stored in the underlying array? Also, is there a way to see the parts and sizes of a slice; to actually see the different components which make it up (the addr, the len, the cap) and each of their respective sizes?
Here is all of the code for what I’m working on:
Thank you for looking at this, and thank you for responding if you respond!
Best,
Todd