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 關注
- 233 瀏覽
添加回答
舉報