Go程序未显示分配给变量的Slice Int的所需结果

My program is having issues with the printRecipeOfTheDay function. Say for example 0 is randomly selected by the program and gets assigned to the Monday variable I created, when I pass "recipe1" to the function "printRecipeOfTheDay" I get no output or a null value. Any idea on what I may have messed up?

        if monday == 0 {
            fmt.Println(0)
            printRecipeOfTheDay(recipe1)
        } else if monday == 1 {
            fmt.Println(1)

The entire program is below:

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
", recipe.name)
    fmt.Printf("Prep Time : %d
", recipe.prepTime)
    fmt.Printf("Cook Time : %d
", recipe.cookTime)
    fmt.Printf("Ingredients : %s
", recipe.Ingredients)
    fmt.Printf("Recipe ID : %d
", recipe.ID)
}

//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
    fmt.Printf("The total time for this recipe is %d
", 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
", i)
    fmt.Printf("%d
", 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
", monday)
    fmt.Printf("%d
", tuesday)
    fmt.Printf("%d
", wednesday)
    fmt.Printf("%d
", thursday)
    fmt.Printf("%d
", 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
", recipe.name)
}

You have your statements in the wrong order. At the point when you try to print the recipe you've declared it but you've done no initialization so it should be 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
", recipe.name)
    fmt.Printf("Prep Time : %d
", recipe.prepTime)
    fmt.Printf("Cook Time : %d
", recipe.cookTime)
    fmt.Printf("Ingredients : %s
", recipe.Ingredients)
    fmt.Printf("Recipe ID : %d
", recipe.ID)
}

//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
    fmt.Printf("The total time for this recipe is %d
", 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
", i)
    fmt.Printf("%d
", 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
", monday)
    fmt.Printf("%d
", tuesday)
    fmt.Printf("%d
", wednesday)
    fmt.Printf("%d
", thursday)
    fmt.Printf("%d
", 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
", recipe.name)
}

I just did a quick cut and paste there cause I'm rather busy at the moment. I may edit this later, it would just be for style and readability. I recommend making an initRecipes method and having it return a []Recipe and using composite literal syntax to do your initialization instead of calling append over and over again.

Thanks to your advice I fixed up my code and it is below. It is working as expected. Now I just have to clean it up more

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
", i)
    fmt.Printf("%d
", i[0])
    fmt.Printf("%d
", 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
", monday)
    fmt.Printf("This is for the day Tuesday %d
", tuesday)
    fmt.Printf("This is for the day Wednesday %d
", wednesday)
    fmt.Printf("This is for the day Thursday %d
", thursday)
    fmt.Printf("This is for the day Friday %d
", 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
", recipe.name)
    fmt.Printf("Prep Time : %d
", recipe.prepTime)
    fmt.Printf("Cook Time : %d
", recipe.cookTime)
    fmt.Printf("Ingredients : %s
", recipe.Ingredients)
    fmt.Printf("Recipe ID : %d
", recipe.ID)
}

//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
    fmt.Printf("The total time for this recipe is %d
", 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
", recipe.name)
}