我的程序遇到printRecipeOfTheDay函数问题。比方说,例如0由程序随机选择并分配给我创建的星期一变量,当我将“recipe1”传递给函数“printRecipeOfTheDay”时,我没有输出或空值。关于我可能搞砸了什么的任何想法?
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
整个计划如下:
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type 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 Recipe
func 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 prepTime
func 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
//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)
}
/* 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
//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 dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
答案 0 :(得分:3)
您的陈述顺序错误。当您尝试打印配方时,您已经声明了它,但是您没有进行初始化,所以它应该为空。
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type 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 Recipe
func 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 prepTime
func 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 dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
我只是快速剪切并粘贴,因为我现在很忙。我可以稍后编辑它,它只是为了风格和可读性。我建议使用initRecipes
方法并让它返回[]Recipe
并使用复合文字语法进行初始化,而不是一遍又一遍地调用append。
答案 1 :(得分:0)
感谢您的建议我修改了我的代码,它位于下方。它按预期工作。现在我只需要清理它了
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type 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 method
func 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 Recipe
func 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 prepTime
func 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 dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}