Composite Types Flashcards
What is an array in Go?
A fixed-size sequence of elements of the same type.
How do you declare an array with zero values in Go?
var x [3]int
initializes to [0 0 0]
.
How do you create an array with specific values in Go?
var x = [3]int{10, 20, 30}
creates [10 20 30]
.
What happens when you access an out-of-bounds index in an array?
It causes a panic.
Why are arrays rarely used directly in Go?
Their fixed size limits flexibility; slices are preferred.
What is a slice in Go?
A dynamic sequence built on an array, with no fixed size.
What is the zero value of a slice?
nil
(no underlying array).
How do you add elements to a slice?
Use append(x, value)
to grow the slice.
What does make([]int, 5, 10)
create?
A slice with length 5, capacity 10, initialized to [0 0 0 0 0]
.
How do you copy a slice in Go?
Use copy(dest, src)
to duplicate elements.
What does len(s)
return for a slice?
The number of elements in the slice.
The number of elements in the slice.
The capacity of the underlying array.
What is a string in Go?
An immutable sequence of bytes, UTF-8 encoded.
What does len(s) return for a string?
The number of bytes, not runes.
What is a rune in Go?
A Unicode code point, which may span multiple bytes.
How do you convert a string to a rune slice?
Use []rune(s)
.
What is a map in Go?
A collection associating keys with values; zero value is nil.
How do you check if a key exists in a map?
Use v, ok := m["key"]
; ok is true if the key exists.
How do you delete a key from a map?
Use delete(m, "key")
.
What is a struct in Go?
A type that groups related data of different types.