create a variable of type float in go

To declare and initialize a variable of type float32 or float64 in Go, use the following syntax:

main.go
var myFloat32 float32 = 3.14
var myFloat64 float64 = 3.1415
60 chars
3 lines

You can also use the shorthand := operator if you want Go to infer the type of the variable:

main.go
myFloat := 3.14        // type inferred as float64
mySmallFloat := 3.14   // type inferred as float32
102 chars
3 lines

Note that if you don't specify the type, Go will automatically default to float64.

related categories

gistlibby LogSnag