饮歌长啸
你的陈述顺序错误。当您尝试打印配方时,您已经声明了它,但您没有进行任何初始化,因此它应该为空。package mainimport ( "fmt" "math/rand" "time")//Struct for Recipe belowtype Recipe struct { //Struct for recipe information name string prepTime int cookTime int Ingredients []string //this is now a slice that will accept multiple elements ID int Yield int}//function to print Recipefunc printRecipe(recipe Recipe) { fmt.Printf("Recipe Name : %s\n", recipe.name) fmt.Printf("Prep Time : %d\n", recipe.prepTime) fmt.Printf("Cook Time : %d\n", recipe.cookTime) fmt.Printf("Ingredients : %s\n", recipe.Ingredients) fmt.Printf("Recipe ID : %d\n", recipe.ID)}//Returns total time by addings cookTime and prepTimefunc totalTime(recipe Recipe) { fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)}func main() { var recipe1 Recipe //Declare recipe1 of Type Recipe var recipe2 Recipe var recipe3 Recipe /* recipe1 specifications */ recipe1.name = "BBQ Pulled Chicken" recipe1.prepTime = 25 recipe1.cookTime = 5 recipe1.Ingredients = append( recipe1.Ingredients, "1 8-ounce can reduced-sodium tomato sauce", ) recipe1.Ingredients = append( recipe1.Ingredients, "1/2 medium onion (grated),", ) recipe1.ID = 1 recipe1.Yield = 8 /* Recipe 2 specifications */ recipe2.name = "Steak Tacos with Pineapple" recipe2.prepTime = 45 recipe2.cookTime = 45 recipe2.Ingredients = append( recipe2.Ingredients, "3 tablespoons soy sauce,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated garlic,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated peeled fresh ginger,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 1/2 pounds skirt steak, cut into 5-inch lengths,", ) recipe2.Ingredients = append( recipe2.Ingredients, "Salt", ) recipe2.Ingredients = append( recipe2.Ingredients, "Pepper", ) recipe2.ID = 2 recipe2.Yield = 4 recipe3.name = "Simple Lemon Herb Chicken" recipe3.prepTime = 10 recipe3.cookTime = 15 recipe3.Ingredients = append( recipe3.Ingredients, "2 skinless boneless chicken breast halves,", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 Lemon,", ) recipe3.Ingredients = append( recipe3.Ingredients, "Salt and Pepper to taste,", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 tablespoon olive oil,", ) recipe3.Ingredients = append( recipe3.Ingredients, "2 sprigs fresh parsley (for garnish),", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 pinch dried oregano,", ) recipe3.ID = 3 recipe3.Yield = 2 //choose random number for recipe r := rand.New(rand.NewSource(time.Now().UnixNano())) i := r.Perm(5) fmt.Printf("%v\n", i) fmt.Printf("%d\n", i[0]) //assign slices of int from Perm to variables assigned days of the week var monday = i[0] var tuesday = i[1] var wednesday = i[2] var thursday = i[3] var friday = i[4] //testing printing of variables assigned to days fmt.Printf("This is for the day Monday %d\n", monday) fmt.Printf("%d\n", tuesday) fmt.Printf("%d\n", wednesday) fmt.Printf("%d\n", thursday) fmt.Printf("%d\n", friday) //logic for Mondays Recipe if monday == 0 { fmt.Println(0) printRecipeOfTheDay(recipe1) } else if monday == 1 { fmt.Println(1) printRecipeOfTheDay(recipe2) } else if monday == 2 { fmt.Println(2) printRecipeOfTheDay(recipe3) } else if monday == 3 { fmt.Println(3) } //call to printRecipe function below printRecipe(recipe1) totalTime(recipe1) printRecipe(recipe2) totalTime(recipe2) printRecipe(recipe3) totalTime(recipe3)}//function to print the winner for recipe of the day to use//for either lunch or dinnerfunc printRecipeOfTheDay(recipe Recipe) { fmt.Printf("The recipe of the day is : %s\n", recipe.name)}我只是在那里快速剪切和粘贴,因为我现在很忙。我可能会在稍后编辑它,这只是为了风格和可读性。我建议创建一个initRecipes方法并让它返回 a[]Recipe并使用复合文字语法来进行初始化,而不是一遍又一遍地调用 append 。
阿晨1998
多亏了你的建议,我修复了我的代码,它在下面。它按预期工作。现在我只需要更多地清理它package mainimport ( "fmt" "math/rand" "time")//Struct for Recipe belowtype Recipe struct { //Struct for recipe information name string prepTime int cookTime int Ingredients []string //this is now a slice that will accept multiple elements ID int Yield int}//main methodfunc main() { //5 variables below for 5 recipes for Monday-Friday var recipe1 Recipe //Declare recipe1 of Type Recipe var recipe2 Recipe var recipe3 Recipe var recipe4 Recipe var recipe5 Recipe //choose random number for recipe r := rand.New(rand.NewSource(time.Now().UnixNano())) i := r.Perm(5) fmt.Printf("%v\n", i) fmt.Printf("%d\n", i[0]) fmt.Printf("%d\n", i[1]) //assign slices of int from Perm to variables assigned days of the week var monday = i[0] var tuesday = i[1] var wednesday = i[2] var thursday = i[3] var friday = i[4] //testing printing of variables assigned to days fmt.Printf("This is for the day Monday %d\n", monday) fmt.Printf("This is for the day Tuesday %d\n", tuesday) fmt.Printf("This is for the day Wednesday %d\n", wednesday) fmt.Printf("This is for the day Thursday %d\n", thursday) fmt.Printf("This is for the day Friday %d\n", friday) /* recipe1 specifications */ recipe1.name = "BBQ Pulled Chicken" recipe1.prepTime = 25 recipe1.cookTime = 5 recipe1.Ingredients = append( recipe1.Ingredients, "1 8-ounce can reduced-sodium tomato sauce", ) recipe1.Ingredients = append( recipe1.Ingredients, "1/2 medium onion (grated),", ) recipe1.ID = 1 recipe1.Yield = 8 /* Recipe 2 specifications */ recipe2.name = "Steak Tacos with Pineapple" recipe2.prepTime = 45 recipe2.cookTime = 45 recipe2.Ingredients = append( recipe2.Ingredients, "3 tablespoons soy sauce,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated garlic,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 tablespoon finely grated peeled fresh ginger,", ) recipe2.Ingredients = append( recipe2.Ingredients, "1 1/2 pounds skirt steak, cut into 5-inch lengths,", ) recipe2.Ingredients = append( recipe2.Ingredients, "Salt", ) recipe2.Ingredients = append( recipe2.Ingredients, "Pepper", ) recipe2.ID = 2 recipe2.Yield = 4 recipe3.name = "Simple Lemon Herb Chicken" recipe3.prepTime = 10 recipe3.cookTime = 15 recipe3.Ingredients = append( recipe3.Ingredients, "2 skinless boneless chicken breast halves,", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 Lemon,", ) recipe3.Ingredients = append( recipe3.Ingredients, "Salt and Pepper to taste,", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 tablespoon olive oil,", ) recipe3.Ingredients = append( recipe3.Ingredients, "2 sprigs fresh parsley (for garnish),", ) recipe3.Ingredients = append( recipe3.Ingredients, "1 pinch dried oregano,", ) recipe3.ID = 3 recipe3.Yield = 2 //recipe4 specifications recipe4.name = "Easy Meatloaf" recipe4.prepTime = 10 recipe4.cookTime = 60 recipe4.Ingredients = append( recipe4.Ingredients, "1 onion (chopped),", ) recipe4.Ingredients = append( recipe4.Ingredients, "1 cup milk,", ) recipe4.Ingredients = append( recipe4.Ingredients, "1 cup dried bread crumbs,", ) recipe4.ID = 4 recipe4.Yield = 8 //recipe 5 specifications recipe5.name = "Fast Salmon with a Ginger Glaze" recipe5.prepTime = 5 recipe5.cookTime = 20 recipe5.Ingredients = append( recipe5.Ingredients, "salt to taste,", ) recipe5.Ingredients = append( recipe5.Ingredients, "1/3 cup cold water,", ) recipe5.Ingredients = append( recipe5.Ingredients, "1/4 cup seasoned rice vinegar,", ) recipe5.ID = 5 recipe5.Yield = 4 //call to printRecipe function below printRecipe(recipe1) totalTime(recipe1) printRecipe(recipe2) totalTime(recipe2) printRecipe(recipe3) totalTime(recipe3) printRecipe(recipe4) totalTime(recipe4) printRecipe(recipe5) totalTime(recipe5) //logic for Mondays Recipe if monday == 0 { fmt.Println(0) printRecipeOfTheDay(recipe1) } else if monday == 1 { fmt.Println(1) printRecipeOfTheDay(recipe2) } else if monday == 2 { fmt.Println(2) printRecipeOfTheDay(recipe3) } else if monday == 3 { fmt.Println(3) printRecipeOfTheDay(recipe4) } else if monday == 4 { fmt.Println(4) printRecipeOfTheDay(recipe5) } //logic for Tuesdays Recipe if tuesday == 0 { fmt.Println(0) printRecipeOfTheDay(recipe1) } else if tuesday == 1 { fmt.Println(1) printRecipeOfTheDay(recipe2) } else if tuesday == 2 { fmt.Println(2) printRecipeOfTheDay(recipe3) } else if tuesday == 3 { fmt.Println(3) printRecipeOfTheDay(recipe4) } else if tuesday == 4 { fmt.Println(4) printRecipeOfTheDay(recipe5) }}//function to print Recipefunc printRecipe(recipe Recipe) { fmt.Printf("Recipe Name : %s\n", recipe.name) fmt.Printf("Prep Time : %d\n", recipe.prepTime) fmt.Printf("Cook Time : %d\n", recipe.cookTime) fmt.Printf("Ingredients : %s\n", recipe.Ingredients) fmt.Printf("Recipe ID : %d\n", recipe.ID)}//Returns total time by addings cookTime and prepTimefunc totalTime(recipe Recipe) { fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)}//function to print the winner for recipe of the day to use//for either lunch or dinnerfunc printRecipeOfTheDay(recipe Recipe) { fmt.Printf("The recipe of the day is : %s\n", recipe.name)}