我有一个这样的类(结构):
type Question struct{ Question string answerOne string answerTwo string answerCorrect string }
我像这样初始化它:
q1:=Question{ Question:"What?", answerOne:"A", answerTwo:"B", answerCorrect: ? //I want this have similar value as `answerOne` }
在初始化时,我希望其中一个值与另一个值具有相似的值。有什么办法吗?
您不能仅使用文字,但可以定义一个函数。
func NewQuestion() *Question { q := &Question{ Question: "What?", answerOne: "A", answerTwo: "B", } q.answerCorrect = q.answerOne return q } // ... q1 := NewQuestion()