Sobes.tech
Junior — Middle

What types of errors can occur when programming in Go?

sobes.tech AI

Answer from AI

In Go (Golang), several types of errors can occur:

  • Syntax errors — errors in the code structure, such as missing brackets or incorrect use of keywords.
  • Runtime errors — for example, division by zero, slice out of bounds, panic.
  • Compilation errors — related to data types, incompatibility, or missing packages.
  • Logic errors — when the program runs but does not do what is intended.
  • Errors returned by functions — in Go, it is common to return errors as values that need to be checked and handled.

Example of error handling in Go:

package main
import (
    "errors"
    "fmt"
)

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10, 0)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Result:", result)
}
What types of errors can occur when programming in… - sobes.tech