Closed williamokano-dh closed 1 year ago
The whole idea here would be to create some flows with just 1 usage of question. If not, that's fine, I can always do several calls with specific objects :)
Hi, as you have guessed, the best way to accomplish this is to make several survey.Ask()
invocations and in between them handle your application's logic to modify questions based on previous responses. Survey has no built-in support for this.
- Is it possible to the "Unmarshalling" of the questions into nested objects? I noticed that is it possible to tag the property very similar on how to json tagging works
No, unlike Go json
package, Survey does not recognize struct field tags (i.e. the `survey:"age"`
bit in your example). Instead, this survey question:
{
Name: "age",
Prompt: &survey.Input{Message: "Age"},
},
will save the answer in the struct field literally named Age
. The first character of the field name is now uppercase because the field needs to be "public" (in Go terms) in order to be writeable from libraries like Survey.
However, the struct field must be string
and not int
. All answers to Survey questions are technically strings, even if they just consist of numbers. The only exception is the Select prompt which can optionally return the index of the selected option, and thus save the answer into an int
receiver.
After the user has entered their age, you can convert their string answer to an integer you can use in Go like so:
person := struct {
Name string
Age string
}{}
err := survey.Ask(qs, &person)
// (handle error)
personAge, err := strconv.Atoi(person.Age)
// (handle error)
Good luck!
Hey, I was playing around with your lib and I got some questions, I'll give some context as well.
First of all, I'm a beginner at golang, so if I say or ask something dumb, forgive me.
But it didn't work. The age is "unmarshalled" as
0
, most likely since it's the default.The whole idea here would be to create some flows with just 1 usage of question. If not, that's fine, I can always do several calls with specific objects :)
E.g.: I want to inquiry drivers licence number only if the age of the person if higher than 16.
Thanks a lot