ficsit-cli-flake/utils/structures.go

21 lines
363 B
Go
Raw Normal View History

2022-05-02 20:07:15 +00:00
package utils
2022-06-06 23:55:26 +00:00
import (
"encoding/json"
"fmt"
2022-06-06 23:55:26 +00:00
)
func Copy[T any](obj T) (*T, error) {
marshal, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("failed to marshal object: %w", err)
2022-05-02 20:07:15 +00:00
}
2022-06-06 23:55:26 +00:00
out := new(T)
if err := json.Unmarshal(marshal, out); err != nil {
return nil, fmt.Errorf("failed to unmarshal object: %w", err)
2022-06-06 23:55:26 +00:00
}
return out, nil
2022-05-02 20:07:15 +00:00
}