I need to string scan 23.584950:58.353603:an address, str:a place, name
into :
type Origin struct {
Lat float64 `json:"lat" binding:"required"`
Lng float64 `json:"lng" binding:"required"`
Address string `json:"address" binding:"required"`
Name string `json:"name" binding:"required"`
}
My code:
package main
import "fmt"
type Origin struct {
Lat float64 `json:"lat" binding:"required"`
Lng float64 `json:"lng" binding:"required"`
Address string `json:"address" binding:"required"`
Name string `json:"name" binding:"required"`
}
func (o *Origin) ParseString(str string) error {
if str == "" {
return nil
}
_, err := fmt.Sscanf(str, "%f:%f:%[^:]:%[^/n]", &o.Lat, &o.Lng, &o.Address, &o.Name)
if err != nil {
return fmt.Errorf("parsing origin: %s, input:%s", err.Error(), str)
}
return nil
}
func main() {
o := &Origin{}
o.ParseString("23.584950:58.353603:address, text:place, name, text")
fmt.Println(o)
}
https://go.dev/play/p/uUTNyx2cDFB
However, struct o prints out: &{23.58495 58.353603 }. Address and name are not scanned properly. What is the correct format to be used in Sscanf to parse this string correctly?