dtlygweb2017 2015-07-25 02:32
浏览 8
已采纳

在外部函数中访问Go结构的值

I have the following function declararion, which works and prints out correctly.

import (
    "fmt"
    "github.com/google/go-github/github"
)

func LatestTag(user, project string) {

    client := github.NewClient(nil)
    releases, _, err := client.Repositories.ListTags(user, project, nil)

    if err != nil {
        fmt.Printf("error: %v
", err)
    } else {
        release := releases[0]
        fmt.Printf("Version: %+v
", *release.Name)
    }

}

EDIT

I have modified the function to return a string (I don't think this is right) but hopefully it can help shed some light on what I am trying to do.

import (
    "fmt"
    "github.com/google/go-github/github"
)

func LatestTag(user, project string) string {

    client := github.NewClient(nil)
    releases, _, err := client.Repositories.ListTags(user, project, nil)
    var release string

    if err != nil {
        fmt.Printf("error: %v
", err)
    } else {
        release := releases[0]
    }
    return *release.Name
}

I would like to return the value of *release.Name rather than just print it out so that I can access the value from another function but I don't understand how returning works in this case (very new to Go).

I was thinking I could just return the struct as a string but get errors when I run it.

release.Name undefined (type string has no field or method Name)

Which makes me think that I'm not approaching this correctly. Can somebody point me in the right direction?

  • 写回答

1条回答 默认 最新

  • dongmin4990 2015-07-25 07:21
    关注

    One problem is here:

    var release string
    ...
    if err != nil {
    ...
    } else {
        release := releases[0]  // <-- here
    }
    

    At the line indicated you define a new variable called release equal to releases[0] which is scoped only to the else clause (use of :=). That then goes out of scope immediately. I'm surprised you don't get an unused variable warning. Looks like you also need to change the type of release to github.RepositoryTag. Try:

    var release github.RepositoryTag
    ...
    if err != nil {
    ...
    } else {
        release = releases[0]  // note equals sign
    }
    

    However a more idiomatic way to do this would be something like (untested):

    func LatestTag(user, project string) (string, error) {
        client := github.NewClient(nil)
        if releases, _, err := client.Repositories.ListTags(user, project, nil); err != nil {
            return "", err
        } else {
            release := releases[0]
            return *release.Name, nil
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示