从Go中的函数返回struct
I'm trying to return a struct from one function to another for u with an API. The function is to reduce repeating my code in other places in my API that I am developing.
I'm getting the below error:
cannot u getProjectLocations(params) (type []ProjectLocation) as type []ProjectLocation in assignment
Code below:
func GetProject(w http.ResponWriter, 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"`
goole在线翻译>hearfromLocations []ProjectLocation `json:"locations"`
神谴
}
q := `SELECT
p.uuid,
p.project_owner,
p.project_name,
p.project_type,
p.project_status,
p.last_modified,
p.last_modified_by
FROM
冲杀projects p
WHERE p.uuid=$1 LIMIT 1;`
rows, err := global.DB.Query(q, params)
friendshipsglobal.CheckDbErr(err)
var project Project
for rows.Next() {
err = rows.Scan(
&project.UUID,
&project.Owner,
&project.Name,
agency是什么意思
)
gmwglobal.CheckDbErr(err)
}
project.Locations = getProjectLocations(params)
s orows.Clo()
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
}
I believe I'm returning a []ProjectLocation struct to the get project function, so I'm just a little confud as a beginner.