fmt.Sscanf用于结构

I have data which was previously serialized as fmt.Sprintf("%+v", d), d being a struct instance. Is there a way to scan it back? My naive attempt of using fmt.Sscanf() below is failing (Go Playground).

package main

import (
    "fmt"
)

type data struct {
    f int
}

func main() {
    x := data{1}
    s := fmt.Sprintf("%+v", x)
    var y data
    n, err := fmt.Sscanf(s, "%+v", &y)
    fmt.Printf("s=%s, n=%d, err=%+v, y=%+v
", s, n, err, y)
}

The output is:

s={f:1}, n=0, err=can't scan type: *main.data, y={f:0}

Is there a way to scan the struct back?

Unfortunately the fmt package doesn't produce strictly parseable output. Thankfully go has other packages for marshaling data which should be just as easy to use.

I'd recommend json for something simple:

s, err := json.Marshal(data)
// Check err
err = json.Unmarshal(s, data)

json will produce output which is somewhat similar to printf, but conforming to a standard which ensures it can be parsed. If your goal is to parse data that exists only in Printf'd form you may be out of luck.