3 回答
TA贡献1883条经验 获得超3个赞
以您的特定示例为例,我可能会执行以下操作:
type neucleotide char // unexported type users can't construct their own.
type DNAStrand []neucleotide // because users can't construct their own
// nucleotides they also can't construct their
// own DNAStrands.
const (
// These are exported values so they can use these nucleotides to construct a
// DNAStrand with.
A nucleotide = 'A'
C nucleotide = 'C'
G nudleotide = 'G'
T nucleotide = 'T'
)
// This function allows them to actually construct a DNAstrand with a list of
// nucleotides from the constants above.
func New(nts ...nucleotide) DNAStrand {
return nts
}
由于不输出核苷酸类型,因此用户无法构建自己的核苷酸。您在导出的const中提供了它们的唯一允许实例,因此没有用户可以提供自己的新核苷酸。
- 3 回答
- 0 关注
- 202 浏览
添加回答
举报