go - How to Add a Slice Array inside of a Struct -
i'm looking add array string variable inside of struct have created in go.
type recipes struct { //struct recipe information name string preptime int cooktime int recipeingredient string recipeid int recipeyield int }
it called by
recipe1.name = "bbq pulled chicken" recipe1.preptime = 25 recipe1.cooktime = 5 recipe1.recipeingredient = "1 8-ounce can reduced-sodium tomato sauce, two" recipe1.recipeid = 1 recipe1.recipeyield = 8
recipeingredient have multiple ingredients can't 1 string. have multiple array/slice elements inside recipeingredient. have idea on how able please?
use slice of string
. example,
package main import "fmt" type recipe struct { name string preptime int cooktime int ingredients []string id int yield int } func main() { var recipe recipe recipe.name = "bbq pulled chicken" recipe.preptime = 25 recipe.cooktime = 5 recipe.ingredients = append(recipe.ingredients, "1 8-ounce can reduced-sodium tomato sauce", ) recipe.ingredients = append(recipe.ingredients, "1/2 medium onion, grated ", ) recipe.id = 1 recipe.yield = 8 fmt.println(recipe) fmt.printf("ingredients: %q\n", recipe.ingredients) }
output:
{bbq pulled chicken 25 5 [1 8-ounce can reduced-sodium tomato sauce 1/2 medium onion, grated ] 1 8} ingredients: ["1 8-ounce can reduced-sodium tomato sauce" "1/2 medium onion, grated "]
Comments
Post a Comment