Go

Go Data Structures

Arrays, slices, maps, structs, pointers, make vs new, nil values

20 面接問題·
Junior
1

What is a slice in Go?

回答

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.

2

What is the main difference between an array and a slice in Go?

回答

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.

3

How to create an empty slice in Go?

回答

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.

4

Which function should be used to initialize a slice with a specific capacity?

5

What happens when adding an element to a slice whose capacity is reached?

+17 面接問題

次の面接に向けてGoをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める