How to Convert the Fahrenheit to Celsius in Golang


Following program shows you how to convert fahrenheit to celsius.

The function FToC below encapsu- lates the temperature conversion logic so that it is defined only once but may be used from multiple places. Here main calls it twice, using the values of two different local constants:

package main

import "fmt"

func main() {

  const freezingF, boilingF = 32.0, 212.0
  fmt.Printf("%g°F = %g°C\n", freezingF, FToC(freezingF)) // "32°F = 0°C"
  fmt.Printf("%g°F = %g°C\n", boilingF, FToC(boilingF))   // "212°F = 100°C"

func FToC(f float64) float64 {
  return (f - 32) * 5 / 9
$ go run main.go
32°F = 0°C
212°F = 100°C

Related Tags