educative.io

GO script times out

package main

import (

“bufio”

“fmt”

“os”

“strconv”

)

// aliasing type

type Celsius float32

type Fahrenheit float32

func main() {

reader := bufio.NewReader(os.Stdin)

t, _ := reader.ReadString(’\n’)

//fmt.Println(toFahrenheit(strconv.ParseFloat(t)))

value, err := strconv.ParseFloat(t, 32)

if err == nil {

fmt.Println(toFahrenheit(Celsius(value)))

}

}

// Function to convert celsius to fahrenheit

func toFahrenheit(t Celsius) Fahrenheit {

//var temp Fahrenheit

temp := Fahrenheit((t * 9 / 5) + 32)

return temp

}

when running this program , it timesout. I am very new to GO . Please suggest

@ravirai
Your code works, but it’s taking longer to execute due to reader.ReadString(‘\n’).
You should improve your code by using ReadLine because ReadString cannot handle the case when the last line of a file doesn’t end with the newline character.

Hope this helps!