Go

Goroutines Basics

Goroutines, go keyword, sync.WaitGroup, runtime.GOMAXPROCS, goroutine leaks

20 perguntas de entrevistaยท
Junior
1

What is a goroutine in Go?

Resposta

A goroutine is a function that executes concurrently with other functions, managed by the Go runtime rather than the OS. It is extremely lightweight (a few KB of initial stack) compared to an OS thread (several MB). The Go scheduler can multiplex thousands or even millions of goroutines onto a limited number of OS threads, making concurrency very accessible in Go.

2

How to start a new goroutine in Go?

Resposta

The go keyword followed by a function call starts a new goroutine. For example, 'go myFunction()' or 'go func() { ... }()' for an anonymous function. The goroutine executes immediately in parallel with the calling code. Warning: the main function does not automatically wait for launched goroutines to finish.

3

What is the main difference between a goroutine and an OS thread?

Resposta

A goroutine consumes about 2 KB of initial stack while an OS thread typically requires 1-2 MB. The Go runtime manages goroutine scheduling in user space (M:N scheduling), multiplexing thousands of goroutines onto a few OS threads. This allows creating millions of goroutines without overloading the system, unlike OS threads which are limited by resources.

4

What happens if the main program terminates before a goroutine finishes?

5

How does the Go runtime manage goroutine execution?

+17 perguntas de entrevista

Domine Go para sua proxima entrevista

Acesse todas as perguntas, flashcards, testes tecnicos, exercicios de code review e simuladores de entrevista.

Comece gratis