2017-08-25 5 views
2

私はいくつかのコードをクリーンアップする過程にあり、構造体であるスライス値を関数に渡そうとしています。スライス要素を関数に渡す方法

私の構造体は次のようになります。私のコードで

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"` 
} 

、私はAPIからいくつかのJSONデータをフェッチし、Dataスライスで約100項目を含むバック応答を取得します。

次に、Dataスライスの各アイテムをループして処理しています。だから私はこのようになりますinsertIntoRecipes関数にレシピのインスタンスを渡すためにしようとしています

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 [email protected]", 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 
    } 
} 

た場合の例としては、それは次のようになります

私はこれを実行し、エラーが発生します:

GetRecipesPaginatedResponse.Data undefined (type GetRecipesPaginatedResponse has no method Data)

私は問題がどのようにnewRecipeinsertIntoRecipes関数newRecipe GetRecipesPaginatedResponse.Dataに渡そうとしているのですか?しかし、それを渡して正しい変数型を宣言する方法はよくわかりません。

Dataスライス内のアイテムを関数に渡すには、Dataスライスの各アイテムをループするとき、どうすればよいですか?

答えて

4

フィールドセレクタを使用してDataフィールドの匿名タイプを参照することはできません。修正はDataフィールドの名前付きの型を宣言することです:

func insertIntoRecipes(db *sql.DB, exists bool, newRecipe DataItem) { 
    ... 
} 

DataItemのためのより良い名前はおそらくあります:

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"` 
    ... 
} 

はこのようにそれを使用してください。

+0

これは魅力的です、ありがとう! – James

関連する問題