Recap - Handling Problems
Part of the Fundamentals section of Coddy's GO journey — lesson 109 of 109.
Challenge
EasyLet's practice error handling in Go with a simple temperature conversion program.
Your task is to implement a function that converts temperatures from Celsius to Fahrenheit, with proper error handling.
Complete the celsiusToFahrenheit function that:
- Converts a Celsius temperature to Fahrenheit using the formula: F = C × 9/5 + 32
- Returns an error if the temperature is below absolute zero (-273.15°C)
For the error message, you must return exactly: "Error: temperature below absolute zero"
The main function contains test cases to verify your implementation.
Try it yourself
package main
import (
"errors"
"fmt"
)
// celsiusToFahrenheit converts Celsius to Fahrenheit
// Returns an error if the temperature is below absolute zero (-273.15°C)
func celsiusToFahrenheit(celsius float64) (float64, error) {
// TODO: Implement the conversion logic
// 1. Check if temperature is below absolute zero (-273.15°C)
// 2. If valid, convert using the formula: F = C × 9/5 + 32
// 3. Return appropriate value and error
return 0, nil
}
func main() {
// Test valid temperature
fmt.Println("Converting 25°C to Fahrenheit:")
fahrenheit, err := celsiusToFahrenheit(25)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("25°C = %.2f°F\n", fahrenheit)
}
// Test another valid temperature
fmt.Println("\nConverting 0°C to Fahrenheit:")
fahrenheit, err = celsiusToFahrenheit(0)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("0°C = %.2f°F\n", fahrenheit)
}
// Test invalid temperature (below absolute zero)
fmt.Println("\nConverting -300°C to Fahrenheit:")
fahrenheit, err = celsiusToFahrenheit(-300)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("-300°C = %.2f°F\n", fahrenheit)
}
}All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code11Composite Types: Slices
Introduction to SlicesDeclaring Slice LiteralsCreating Slices with `make`Slice Length vs CapacityAccessing Slice ElementsAppending ElementsSlicing Existing Slices/ArraysIterating Over SlicesCopying SlicesRecap - Dynamic Lists14Basic Error Handling
The Concept of ErrorsThe `error` InterfaceFunctions Returning ErrorsChecking for ErrorsCreating Simple ErrorsCreating Formatted ErrorsBasic Error HandlingRecap - Handling Problems3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions