I was playing around with Go and was wondering what the best way is to perform idiomatic type conversions in Go. Basically my problem lays within automatic type conversions between uint8
, uint64
, and float64
. From my experience with other languages a multiplication of a uint8
with a uint64
will yield a uint64
value, but not so in go.
Here is an example that I build and I ask if this is the idiomatic way of writing this code or if I'm missing an important language construct.
package main
import ("math";"fmt")
const(Width=64)
func main() {
var index uint32
var bits uint8
index = 100
bits = 3
var c uint64
// This is the line of interest vvvv
c = uint64(math.Ceil(float64(index * uint32(bits))/float64(Width)))
fmt.Println("Test: %v\n", c)
}
From my point of view the calculation of the ceiling value seems unnecessary complex because of all the explicit type conversions.
Thanks!
float64(b[0])
to set an uint8/byte to a float64. See https://mcmap.net/q/56302/-how-to-convert-uint8-to-string.byte(f)
to set a float64 to an uint8/byte. See https://mcmap.net/q/56303/-how-to-round-to-nearest-int-when-casting-float-to-int-in-go. – Respond