
Go Data Structures
Arrays, slices, maps, structs, pointers, make vs new, nil values
1What is a slice in Go?
What is a slice in Go?
Câu trả lời
A slice is a dynamic view over an underlying array that allows manipulation of variable-length sequences of elements. Unlike arrays which have a fixed size defined at compile time, slices can grow or shrink dynamically through operations like append. This flexibility makes them the most commonly used sequential data structure in Go for storing collections of homogeneous elements.
2What is the main difference between an array and a slice in Go?
What is the main difference between an array and a slice in Go?
Câu trả lời
The fundamental difference lies in size: an array has a fixed size defined at compile time and is part of the type itself, while a slice has a dynamic size that can change at runtime. This distinction means that two arrays of different sizes are considered incompatible types, whereas all slices of the same element type are compatible. Slices are therefore more flexible and more commonly used in modern Go code.
3How to create an empty slice in Go?
How to create an empty slice in Go?
Câu trả lời
The literal syntax with empty brackets followed by empty braces is the idiomatic way to create an initialized empty slice. This approach creates a slice of length 0 but non-nil, ready to receive elements via append. Unlike a simple declaration which would yield a nil slice, this explicit initialization guarantees a slice usable immediately without risk of unexpected behavior when adding elements.
Which function should be used to initialize a slice with a specific capacity?
What happens when adding an element to a slice whose capacity is reached?
+17 câu hỏi phỏng vấn
Các chủ đề phỏng vấn Go khác
Go Basics
Go Interfaces
Error Handling
Goroutines Basics
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
database/sql
Context Package
Testing
Concurrency Patterns
Sync Primitives
Go Web Frameworks
REST API Design
gRPC
Reflection
Memory Management
Performance Optimization
Generics
Go Design Patterns
Microservices
Security & Authentication
Docker & Containerization
Kubernetes Basics
Advanced Go
CLI Development
Nắm vững Go cho lần phỏng vấn tiếp theo
Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.
Bắt đầu miễn phí