我正在尝试将一个结构从一个函数返回到另一个函数以与 API 一起使用。该功能是为了减少我正在开发的 API 中其他地方的重复代码。我收到以下错误:无法在分配中使用 getProjectLocations(params) (类型 []ProjectLocation)作为类型 []ProjectLocation代码如下:func GetProject(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r)["uuid"] type ProjectLocation struct { UUID string `json:"uuid"` Location string `json:"location"` Primary bool `json:"is_primary"` } type Project struct { UUID string `json:"uuid"` Owner null.String `json:"project_owner"` Name string `json:"project_name"` Locations []ProjectLocation `json:"locations"` } q := `SELECT p.uuid, p.project_owner, p.project_name, p.project_type, p.project_status, p.created_date, p.created_by, p.last_modified, p.last_modified_by FROM projects p WHERE p.uuid=$1 LIMIT 1;` rows, err := global.DB.Query(q, params) global.CheckDbErr(err) var project Project for rows.Next() { err = rows.Scan( &project.UUID, &project.Owner, &project.Name, ) global.CheckDbErr(err) } project.Locations = getProjectLocations(params) rows.Close() json.NewEncoder(w).Encode(project)}func getProjectLocations(uuid string) []ProjectLocation { var Locations []ProjectLocation q := `SELECT uuid,location,is_primary FROM project_locations WHERE project_uuid=$1` rows, err := global.DB.Query(q, uuid) global.CheckDbErr(err) for rows.Next() { var location ProjectLocation err = rows.Scan( &location.UUID, &location.Location, &location.Primary, ) Locations = append(Locations, location) } return Locations}我相信我正在将一个[]ProjectLocation结构返回给 get project 函数,所以作为初学者我有点困惑。
1 回答
桃花长相依
TA贡献1860条经验 获得超8个赞
func GetProject(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)["uuid"]
type ProjectLocation struct {
UUID string `json:"uuid"`
Location string `json:"location"`
Primary bool `json:"is_primary"`
}
// ...
}
func getProjectLocations(uuid string) []ProjectLocation {
// ...
}
type ProjectLocation是本地的func GetProject.
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
提交
取消