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, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기