Reputation: 313
I have the following code:
buffer := make([]byte, 256)
conn.Read(buffer)
result:= string(buffer)
fmt.Println("Val:", result)
floatResult, _ := strconv.ParseFloat(result, 64)
fmt.Println("Val on float:", floatResult)
The output is the following:
Val: 40.385167
Val on float: 0
This seems the correct way to parse string into float64
's, is there something i'm missing?
Upvotes: 1
Views: 888
Reputation: 4515
You can create new slice with len
equals read bytes (look at Reader)
buffer := make([]byte, 256)
i, _ := conn.Read(buffer) // i 👉🏻 number of read bytes
result:= string(buffer[:i]) // create new slice with 'len' equals number of read bytes
fmt.Println("Val:", result)
floatResult, _ := strconv.ParseFloat(result, 64)
fmt.Println("Val on float:", floatResult)
Upvotes: 1