谁能提供您自己/标准的“克隆方法”惯例,以供我/我们参考?

I can't find a "clone" method convention in Golang, but it seems necessary to have one.

I only saw the built-in way *clonedObj = *obj, but it is too low-level, and can't handle (when-necessary) deep copy of case like struct { member *CompositionObj }

I doubt whether "func (obj ClassA) Clone() interface{}" prototype will work, because calling obj2 := obj.Clone() will "loose" the method set for ClassA, and need explicit code like obj2.(*ClassA) afterwards.

Please advice a working direction.

Ok, having some while no one else give me proper reference, I have found out some reference example how to clone in Go myself and want to share.

(Only upvote me a few if this answer is useful to you. I'm not for earning votes. Welcome other better answers and comments)

I found this protoype in package "github.com/jinzhu/gorm" (Database's ORM library) for reference:

func (s *DB) clone() *DB {
    db := &DB{
        ...
    }
    ...
    return db
}

And similar pattern in package "golang.org/x/net/html/atom":

func (n *Node) clone() *Node {
    m := &Node{
        Type:     n.Type,
        ...
    }
    ...
    return m
}

The above prototype is enough if the Clone()'s caller always know your object type when cloning. (and you need uppercase Clone() to make the method to be "public")

However, if you want advanced feature that a variable may hold any object of similar base interface, here is my sample:

func (t *T) Clone() YourBaseInterface

Where YourBaseInterface is:

type YourBaseInterface interface {
    Clone() YourBaseInterface
    OtherMethod1()
    ...
}

Or can merely use interface{} instead of YourBaseInterface in the return, and do a typecast like obj2 := obj.Clone().(*YourBaseType) after clone.

CAUTION

There is one drawback with this prototype. Becase Golang doesn't support this prototype as build-in, the Clone() method won't be called in some language's feature, e.g. when you copy(dest, src) a []YourTypeWithClone slice. Instead, it still do plain *elem2 = *elem1 struct copying. Solutions maybe either don't use those build-in, or you may flaw back to design the class struct members so that doing plain copy is enough for its copy purpose if possible.

This answer to a similar question regarding maps suggests to use the gob package. The documentation states:

A stream of gobs is self-describing. Each data item in the stream is preceded by a specification of its type, expressed in terms of a small set of predefined types. Pointers are not transmitted, but the things they point to are transmitted; that is, the values are flattened. Nil pointers are not permitted, as they have no value. Recursive types work fine, but recursive values (data with cycles) are problematic. This may change.

so it may not be suitable for your use case.

That said, your question largely depends on your actual use-case. You do not need a generic way to deep-copy things usually, you can usually either get away with the built-in copy mechanics or write concrete copy functions for the types that actually need it.

An alternative might be the deepcopy package but I have no experience with it myself, I just found it on Goolge.