Go 如何将切片元素传递给函数

Go 如何将切片元素传递给函数,go,struct,Go,Struct,我正在清理一些代码,并试图将一个切片值(结构)传递给函数 我的结构如下所示: type GetRecipesPaginatedResponse struct { Total int `json:"total"` PerPage int `json:"per_page"` CurrentPage int `json:"current_page"` LastPage int `j

我正在清理一些代码,并试图将一个切片值(结构)传递给函数

我的结构如下所示:

type GetRecipesPaginatedResponse struct {
    Total       int         `json:"total"`
    PerPage     int         `json:"per_page"`
    CurrentPage int         `json:"current_page"`
    LastPage    int         `json:"last_page"`
    NextPageURL string      `json:"next_page_url"`
    PrevPageURL interface{} `json:"prev_page_url"`
    From        int         `json:"from"`
    To          int         `json:"to"`
    Data        []struct {
        ID               int       `json:"id"`
        ParentRecipeID   int       `json:"parent_recipe_id"`
        UserID           int       `json:"user_id"`
        Name             string    `json:"name"`
        Description      string    `json:"description"`
        IsActive         bool      `json:"is_active"`
        IsPublic         bool      `json:"is_public"`
        CreatedAt        time.Time `json:"created_at"`
        UpdatedAt        time.Time `json:"updated_at"`          
        BjcpStyle struct {
            SubCategoryID   string `json:"sub_category_id"`
            CategoryName    string `json:"category_name"`
            SubCategoryName string `json:"sub_category_name"`
        } `json:"bjcp_style"`
        UnitType struct {
            ID   int    `json:"id"`
            Name string `json:"name"`
        } `json:"unit_type"`
    } `json:"data"`
}
for page := 1; page < 3; page++ {
    newRecipes := getFromRecipesEndpoint(page, latestTimeStamp) //this returns an instance of GetRecipesPaginatedResponse

    for _, newRecipe := range newRecipes.Data {

        if rowExists(db, "SELECT id from community_recipes WHERE id=@id", sql.Named("id", newRecipe.ID)) {
            insertIntoRecipes(db, true, newRecipe)
        } else {
            insertIntoRecipes(db, false, newRecipe)
        }
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe GetRecipesPaginatedResponse.Data) {
    if exists {
        //update the existing record in the DB
        //perform some other updates with the information
    } else {
        //insert a new record into the DB
        //insert some other information using this information
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe DataItem) {
    ...
}
在我的代码中,我从一个API获取一些JSON数据,并得到一个在
data
slice中包含大约100项的响应

然后我在
数据
切片中的每个项目上循环并处理它们

例如,它看起来像这样:

type GetRecipesPaginatedResponse struct {
    Total       int         `json:"total"`
    PerPage     int         `json:"per_page"`
    CurrentPage int         `json:"current_page"`
    LastPage    int         `json:"last_page"`
    NextPageURL string      `json:"next_page_url"`
    PrevPageURL interface{} `json:"prev_page_url"`
    From        int         `json:"from"`
    To          int         `json:"to"`
    Data        []struct {
        ID               int       `json:"id"`
        ParentRecipeID   int       `json:"parent_recipe_id"`
        UserID           int       `json:"user_id"`
        Name             string    `json:"name"`
        Description      string    `json:"description"`
        IsActive         bool      `json:"is_active"`
        IsPublic         bool      `json:"is_public"`
        CreatedAt        time.Time `json:"created_at"`
        UpdatedAt        time.Time `json:"updated_at"`          
        BjcpStyle struct {
            SubCategoryID   string `json:"sub_category_id"`
            CategoryName    string `json:"category_name"`
            SubCategoryName string `json:"sub_category_name"`
        } `json:"bjcp_style"`
        UnitType struct {
            ID   int    `json:"id"`
            Name string `json:"name"`
        } `json:"unit_type"`
    } `json:"data"`
}
for page := 1; page < 3; page++ {
    newRecipes := getFromRecipesEndpoint(page, latestTimeStamp) //this returns an instance of GetRecipesPaginatedResponse

    for _, newRecipe := range newRecipes.Data {

        if rowExists(db, "SELECT id from community_recipes WHERE id=@id", sql.Named("id", newRecipe.ID)) {
            insertIntoRecipes(db, true, newRecipe)
        } else {
            insertIntoRecipes(db, false, newRecipe)
        }
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe GetRecipesPaginatedResponse.Data) {
    if exists {
        //update the existing record in the DB
        //perform some other updates with the information
    } else {
        //insert a new record into the DB
        //insert some other information using this information
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe DataItem) {
    ...
}
当我运行此操作时,我会得到以下错误:

GetRecipesPaginatedResponse.Data未定义(GetRecipesPaginatedResponse类型没有方法数据)

我可以看出问题在于如何将
newRecipe
传递给
insertintocipes
函数,
newRecipe getrecipes paginatedresponse.Data
,但是我不太确定如何传递它并声明正确的变量类型


若要将
数据
切片中的项目传递给函数,当我在
数据
切片的每个项目上循环时,我该如何做?

您不能使用字段选择器引用
数据
字段的匿名类型。修复方法是为
数据
字段声明命名类型:

type GetRecipesPaginatedResponse struct {
    ...
    Data        []DataItem
    ...
}

type DataItem struct {
    ID               int       `json:"id"`
    ParentRecipeID   int       `json:"parent_recipe_id"`
    UserID           int       `json:"user_id"`
    Name             string    `json:"name"`
    ...
}
像这样使用它:

type GetRecipesPaginatedResponse struct {
    Total       int         `json:"total"`
    PerPage     int         `json:"per_page"`
    CurrentPage int         `json:"current_page"`
    LastPage    int         `json:"last_page"`
    NextPageURL string      `json:"next_page_url"`
    PrevPageURL interface{} `json:"prev_page_url"`
    From        int         `json:"from"`
    To          int         `json:"to"`
    Data        []struct {
        ID               int       `json:"id"`
        ParentRecipeID   int       `json:"parent_recipe_id"`
        UserID           int       `json:"user_id"`
        Name             string    `json:"name"`
        Description      string    `json:"description"`
        IsActive         bool      `json:"is_active"`
        IsPublic         bool      `json:"is_public"`
        CreatedAt        time.Time `json:"created_at"`
        UpdatedAt        time.Time `json:"updated_at"`          
        BjcpStyle struct {
            SubCategoryID   string `json:"sub_category_id"`
            CategoryName    string `json:"category_name"`
            SubCategoryName string `json:"sub_category_name"`
        } `json:"bjcp_style"`
        UnitType struct {
            ID   int    `json:"id"`
            Name string `json:"name"`
        } `json:"unit_type"`
    } `json:"data"`
}
for page := 1; page < 3; page++ {
    newRecipes := getFromRecipesEndpoint(page, latestTimeStamp) //this returns an instance of GetRecipesPaginatedResponse

    for _, newRecipe := range newRecipes.Data {

        if rowExists(db, "SELECT id from community_recipes WHERE id=@id", sql.Named("id", newRecipe.ID)) {
            insertIntoRecipes(db, true, newRecipe)
        } else {
            insertIntoRecipes(db, false, newRecipe)
        }
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe GetRecipesPaginatedResponse.Data) {
    if exists {
        //update the existing record in the DB
        //perform some other updates with the information
    } else {
        //insert a new record into the DB
        //insert some other information using this information
    }
}
func insertIntoRecipes(db *sql.DB, exists bool, newRecipe DataItem) {
    ...
}
对于
DataItem
,可能有一个更好的名称