我需要将字符串扫描23.584950:58.353603:an address, str:a place, name 成:
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"`
}
我的代码:
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
但是,structo打印出:&{23.58495 58.353603 }。地址和姓名未正确扫描。Sscanf正确解析此字符串的正确格式是什么?
慕妹3242003
相关分类