This is a continuation of Converting Celsius to Fahrenheit with Python, and TypeScript, but in Go:


package main

import (
    "fmt"
    "math"
)

func c2f(c int) float64 {
    return float64(c)*9/5 + 32
}

func isMirror(a int, b int) bool {
    return reverseString(massage(a)) == massage(b)
}

func massage(n int) string {
    switch {
    case n < 10:
        return fmt.Sprintf("0%d", n)
    case n >= 100:
        return massage(n - 100)
    default:
        return fmt.Sprintf("%d", n)
    }
}

func reverseString(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

func printConversion(c int, f int) {
    fmt.Println(fmt.Sprintf("%d°C ~= %d°F", c, f))
}

func main() {
    for c := 4; c < 100; c += 12 {
        var f = c2f(c)
        if isMirror(c, int(math.Ceil(f))) {
            printConversion(c, int(math.Ceil(f)))
        } else if isMirror(c, int(math.Floor(f))) {
            printConversion(c, int(math.Floor(f)))
        } else {
            break
        }
    }
}

Run it like this:


go run conversion.go

or build and run:


go build -o conversion-go conversion.go
./conversion-go

and the output becomes:

4°C ~= 40°F
16°C ~= 61°F
28°C ~= 82°F
40°C ~= 104°F
52°C ~= 125°F

Comments

Your email will never ever be published.

Previous:
Converting Celsius to Fahrenheit with TypeScript July 16, 2024 JavaScript, Bun
Next:
Converting Celsius to Fahrenheit with Ruby July 18, 2024 Ruby
Related by category:
Converting Celsius to Fahrenheit round-up July 22, 2024 Go
Unzip benchmark on AWS EC2 c3.large vs c4.large November 29, 2017 Go
Go vs. Python October 24, 2014 Go
My favorite Go multiplexer January 28, 2015 Go
Related by keyword:
Comparing Deno vs Node vs Bun August 5, 2024 JavaScript, Bun
Converting Celsius to Fahrenheit round-up July 22, 2024 Python, Go, Node, JavaScript, Bun, Ruby, Rust
Converting Celsius to Fahrenheit with Python July 12, 2024 Python
Converting Celsius to Fahrenheit with Rust July 20, 2024 Rust