Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm Go程序未显示分配给变量的切片整数的所需结果_Algorithm_Function_Go - Fatal编程技术网

Algorithm Go程序未显示分配给变量的切片整数的所需结果

Algorithm Go程序未显示分配给变量的切片整数的所需结果,algorithm,function,go,Algorithm,Function,Go,我的程序在printRecipeOfTheDay函数中出现问题。例如,程序随机选择0并将其分配给我创建的星期一变量,当我将“recipe1”传递给函数“printRecipeOfTheDay”时,我没有得到任何输出或空值。知道我搞砸了什么吗 if monday == 0 { fmt.Println(0) printRecipeOfTheDay(recipe1) } else if monday == 1 {

我的程序在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)
}

你的陈述顺序不对。当您试图打印配方时,您已经声明了它,但没有进行初始化,因此它应该为null

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
方法,让它返回一个
[]配方
,并使用复合文字语法进行初始化,而不是反复调用append。

您的语句顺序错误。当您试图打印配方时,您已经声明了它,但没有进行初始化,因此它应该为null

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
方法,让它返回一个
[]配方
,并使用复合文字语法来进行初始化,而不是一次又一次地调用append。

感谢您的建议,我修复了我的代码,如下所示。它正在按预期工作。现在我只需要把它清理干净

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)
}

感谢你的建议,我修正了我的代码,如下所示。它正在按预期工作。现在我只需要把它清理干净

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)
}

想想调用代码时recipe1的值是多少;在代码片段之后初始化它。啊,我明白了,所以把函数移到顶部?想想在调用代码时recipe1的值是多少;在那段代码之后初始化它。啊,我明白了,所以把函数移到顶部?