On Tue, Jan 25, 2022 at 11:35 AM Victor Giordano <vitucho3...@gmail.com> wrote:
>
> Hi all! I'm dealing with some multi valued column in postgres, and i have 
> stomp into a bizzare behavoir in `fmt.Sscanf`
>
> Please take a look at the following snippet, i also leave my question within 
> the code and this link to the playground so you can actually try it and see 
> with you own eyes.
>
> ```
> package main
>
> import "fmt"
>
> func main() {
>         var a int
>         var b string
>         input := "(1,beto)" // doesn't like the parenthesis
>
>         _, err := fmt.Sscanf(input, "(%d,%s)", &a, &b)
>         if err != nil {
>                 fmt.Println(err)
>         } else {
>                 fmt.Println(a, b)
>         }
>
>         input = "1,beto" // this works
>         _, err = fmt.Sscanf(input, "%d,%s", &a, &b)
>         if err != nil {
>                 fmt.Println(err)
>         } else {
>                 fmt.Println(a, b)
>         }
>
>         input = "beto,1" // doesn't like an string at first place, ¿¿WHAT i 
> do?!?!
>         _, err = fmt.Sscanf(input, "%s,%d", &b, &a)
>         if err != nil {
>                 fmt.Println(err)
>         } else {
>                 fmt.Println(a, b)
>         }
> }
> ```


Using %s in fmt.Sscanf will read all characters up to a space or
newline.  This is documented at https://pkg.go.dev/fmt which says
"Input processed by verbs is implicitly space-delimited: the
implementation of every verb except %c starts by discarding leading
spaces from the remaining input, and the %s verb (and %v reading into
a string) stops consuming input at the first space or newline
character."

Basically fmt.Sscanf is not the right tool for the problem you are
trying to solve.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUtnuM8D2qBLvx8n2PqZUsKCk7wM0%3DiYiOKimzDpVZAbQ%40mail.gmail.com.

Reply via email to