1

I have a class (struct) like this:

type Question struct{
    Question string
    answerOne string
    answerTwo string
    answerCorrect string
}

And I initialize it like this:

q1:=Question{
    Question:"What?",
    answerOne:"A",
    answerTwo:"B",
    answerCorrect: ? //I want this have similar value as `answerOne`
}

While initilizing I want one of my values have similar value as another one. Is there any way to do that?

icza
  • 389,944
  • 63
  • 907
  • 827
  • No of course not. What makes you think this should be possible? – Volker Dec 08 '17 at 09:56
  • coz it is very useful tool. However I have never seen such thing in other languages. I thought, perhaps google developers did that. –  Dec 08 '17 at 13:38

3 Answers3

3

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()
mkopriva
  • 35,176
  • 4
  • 57
  • 71
1

Analog to setting the correctAnswer field after creating the Question value, you can store the correct answer in a variable prior, and use that twice in the composite literal:

answerA := "A"
q := Question{
    Question:      "What?",
    answerOne:     answerA,
    answerTwo:     "B",
    answerCorrect: answerA,
}
icza
  • 389,944
  • 63
  • 907
  • 827
-2

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)
}
Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • I think that @mkopriva's solution is more elegant than `question.answerCorrect = question.answerTwo` directly inside `main` – V. Panchenko Dec 08 '17 at 10:12
  • It is just an option you can do by both that does not mean you need to create a function. – Himanshu Dec 08 '17 at 10:13
  • you **should** create a function to hide some specific things like "setting value to `answerCorrect`", because programmer which will use your code know nothing about this requirement. – V. Panchenko Dec 08 '17 at 10:16
  • I think you misjudged the question here. You are correct but looking at the question Bungler hadn't asked for any specific requirement. – Himanshu Dec 08 '17 at 10:18
  • Thank you, but `answerCorrect` it is a "specific requirement" and @Bungler should create a function to protect programmers, which will use this code, from "underwater rocks" of this code. Who knows that after creating "Question" object he should set value to `answerCorrect` field? - no one. So if @Bungler will go by my way it will have more clean code, than if he go by your way :) – V. Panchenko Dec 08 '17 at 10:23
  • **typo in the last sentence in previous comment**: So if @Bungler chooses my solution he will have a cleaner code than if he chooses your solution – V. Panchenko Dec 08 '17 at 10:30