Junior — Middle
Kuidas saab Go keeles programmi käitumist selle täitmise ajal juhtida?
sobes.tech AI
Vastus AI-lt
In the Go language, controlling the program's behavior at runtime can be achieved in several ways:
- Conditional operators (
if,switch) — allow executing different blocks of code depending on conditions. - Loops (
for) — for repeating actions. - Error handling using return values and the
if err != nilstructure. - Panic and recovery (
panicandrecover) — for handling critical errors and recovering from them. - Goroutines and channels — for managing parallelism and synchronization.
- Interfaces and polymorphism — for changing behavior through different implementations.
Example of using a conditional operator and error handling:
if err := doSomething(); err != nil {
fmt.Println("Error:", err)
return
}
if value > 10 {
fmt.Println("Value greater than 10")
} else {
fmt.Println("Value 10 or less")
}