Golang : How to read float value from standard input ?
This is a continuation from previous tutorial on how to read integer value from standard input. For this tutorial, we will learn how to read float value from standard input. Below is the code to do that :
package main
import (
"fmt"
"strconv"
)
func main() {
var f float64
fmt.Println("Enter a float value : ")
_, err := fmt.Scanf("%f", &f)
if err != nil {
fmt.Println(err)
}
fmt.Printf("You have entered : %f \n", f)
fmt.Println("Alternative output ", strconv.FormatFloat(f, 'f', 6, 64))
}
See also : Golang : How to read integer value from standard input ?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.7k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+27k Get file path of temporary file in Go
+9.9k Golang : Roll the dice example
+16.1k Golang : How to tell if a file is compressed either gzip or zip ?
+17.4k Golang : How to get hour, minute, second from time?
+23.5k Golang : GORM read from database example
+18k Golang : Populate dropdown with html/template example
+5.9k PHP : Shuffle to display different content or advertisement
+6.1k Golang : constant 20013 overflows byte error message
+32.9k Golang : Proper way to set function argument default value
+13.2k Golang : Chunk split or divide a string into smaller chunk example
+9.4k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)