dongqiaochi2711 2019-08-28 04:28
浏览 1091
已采纳

从Go中的函数返回struct

I'm trying to return a struct from one function to another for use 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 use getProjectLocations(params) (type []ProjectLocation) as type []ProjectLocation in assignment

Code below:

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
}

I believe I'm returning a []ProjectLocation struct to the get project function, so I'm just a little confused as a beginner.

  • 写回答

1条回答 默认 最新

  • douba8758 2019-08-28 04:38
    关注
    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 is local to func GetProject.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。