ficsit-cli-flake/utils/structures.go
2022-06-07 02:55:26 +03:00

21 lines
376 B
Go

package utils
import (
"encoding/json"
"github.com/pkg/errors"
)
func Copy[T any](obj T) (*T, error) {
marshal, err := json.Marshal(obj)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal object")
}
out := new(T)
if err := json.Unmarshal(marshal, out); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal object")
}
return out, nil
}