skip to Main Content

I’m getting coordinates (location) as an output of 2 float64 numbers, and it looks like this:

&{%!s(float64=42.539679) %!s(float64=42.601339)}

This is the first time I’m seeing anything like that, so what is “%!s”?
“TypeOf” says “%!s(float64=42.539679)” is float64. So how do I work with this kind of floats? Is there any way to parse it, or somehow to make the %!s(float64=42.539679) look like 42.539679?

UPD: the highlighted line is a *tgbotapi.Location object from Syfaro’s telegram bot api.
The api has this structure:

type Location struct {
    Longitude float64 `json:"longitude"`
    Latitude  float64 `json:"latitude"`
}

and the Location.Latitude gives me this: “%!s(float64=42.539679)” (float64)(?)

2

Answers


  1. https://golang.org/pkg/fmt/

    %!s is basically used in errors to help you identify a problem.

    Login or Signup to reply.
  2. I think this is a matter of using an incorrect format “verb.” You need to use %f instead of %s

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        var f float64 = 3.14
        fmt.Printf("attempting to print as string: %s n", f)
        fmt.Printf("attempting to print as float: %f", f)
    }
    

    Runnable: https://play.golang.org/p/Pec_QrxBIl

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search