我有一個方法,它獲取用戶 ID 列表并返回用戶詳細信息列表。func ListUser(userIDs []interface{}) (users []User, err error) { // query on DB based on userID and return list of users return users, nil}現在我想為其公開一個 API 端點。因此,我嘗試從我的 URL 獲取用戶 ID。func ListUserProfile(w http.ResponseWriter, r *http.Request) { // I know I can get the single value using //r.URL.Query().Get("user-id") // but here List user takes []interface{} as a argument users, err := users.ListUser(userIDs)}有什么方法可以從我的 URL 獲取 userIDS 列表嗎?
1 回答

慕容708150
TA貢獻1831條經驗 獲得超4個贊
解析表單并將[]string
表單中的內容復制到[]interface{}
.?
r.ParseForm()
userIDs := make([]interface{}, len(r.Form["user-id"]))
for i, s := range r.Form["user-id"] {
? ?userIDs[i] = s
}
users, err := users.ListUser(userIDs)
(問題和評論對查詢參數的名稱不一致。請調整答案中的代碼以匹配實際名稱。)
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消