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

后端 未结 3 818
佛祖请我去吃肉
佛祖请我去吃肉 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 can create a pointer to struct and then assign the value to struct variable answerCorrect. Finally you can print the valueAt struct pointer.

    package main
    
    import "fmt"
    
    // Question struct for content
    type Question struct {
        Question      string
        answerOne     string
        answerTwo     string
        answerCorrect string
    }
    
    func main() {
        question := &Question{
            Question:  "What?",
            answerOne: "A",
            answerTwo: "B",
        }
    
        // apply condition to chose from the answer
    
        question.answerCorrect = question.answerTwo
        fmt.Println(*question)
    }
    

提交回复
热议问题