Go

Go Design Patterns

Singleton, Factory, Builder, Strategy, Decorator, Adapter, functional options, SOLID

24 面接問題·
Senior
1

What is the main advantage of the Functional Options pattern in Go?

回答

The Functional Options pattern allows creating configurable APIs with optional parameters without overloading constructors. It avoids multiple constructors with different signatures and provides an extensible and readable API. This pattern is idiomatic in Go because it leverages first-class functions and avoids ambiguous default values.

2

What approach is recommended for implementing the Singleton pattern in Go?

回答

In Go, the Singleton pattern is ideally implemented with sync.Once which guarantees thread-safe and lazy initialization. This approach avoids manual mutexes and double-checked locking which are error-prone. sync.Once ensures that a function is executed only once even with multiple concurrent goroutines.

3

Which SOLID principle encourages the use of interfaces in Go?

回答

The Dependency Inversion principle states that high-level modules should not depend on low-level modules, but on abstractions (interfaces). In Go, this translates to accepting interfaces and returning concrete types. This principle facilitates testing (mocking), decoupling and code maintainability.

4

How to implement the Factory pattern in Go idiomatically?

5

What is the main role of the Adapter pattern?

+21 面接問題

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

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

無料で始める