Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a Go program to implement a custom error using the errors.New() function.

Medium

Problem Statement

Write a Go program to implement a custom error using the errors.New() function.

Sample Input

A predefined condition that triggers the error.

Sample Output

Error: value cannot be negative

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
package main import ("errors" "fmt") func checkValue(value int) error { // Return an error if the value is less than 0 return nil } func main() { // Check the value and handle the error }
Log in or sign up to run or submit your code.Log inSign up
Input
A predefined condition that triggers the error.
Expected Output
Error: value cannot be negative