Can I bind one element of class to another while initializing in one line?

后端 未结 3 896
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 08:27

I have a class (struct) like this:

type Question struct{
    Question string
    answerOne string
    answerTwo string
    answerCorrect string
}
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 08:36

    You cannot by using only literals, but you could define a function.

    func NewQuestion() *Question {
        q := &Question{
            Question:  "What?",
            answerOne: "A",
            answerTwo: "B",
        }
        q.answerCorrect = q.answerOne
        return q
    }
    
    // ...
    
    q1 := NewQuestion()
    

提交回复
热议问题