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?

图片转代码服务由CSDN问答提供 功能建议

我有以下函数声明,该函数可以正常工作并正确打印出来。

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

func LatestTag(用户,项目字符串){
 
 client:= github。  NewClient(nil)
发布,_,err:= client.Repositories.ListTags(user,project,nil)
 
 if err!= nil {
 fmt.Printf(“错误:%v 
”,  err)
} else {
 release:= releases [0] 
 fmt.Printf(“ Version:%+ v 
”,* release.Name)
} 
 
} 
   
 
 

编辑

我修改了返回字符串的功能(我认为这不对) 但希望它可以帮助我了解我要做什么。

  import(
“ fmt” 
“ github.com/google/go-github/  github“ 
)
 
func LatestTag(用户,项目字符串)字符串{
 
 client:= github.NewClient(nil)
版本,_,err:= client.Repositories.ListTags(用户,项目,  nil)
 var rel 缓和字符串
 
,如果出错!= nil {
 fmt.Printf(“错误:%v 
”,err)
}否则{
 release:= releases [0] 
} 
返回*  release.Name 
} 
   
 
 

我想返回 * release.Name 的值,而不是仅仅打印出来以便 我可以从另一个函数访问该值,但在这种情况下我不了解返回的工作方式(这是Go的新手)。

我当时想我可以将结构作为字符串返回

  release.Name未定义(类型字符串没有字段或方法名称)
   
 
  

这让我觉得我没有正确地做到这一点。 有人可以指出我正确的方向吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题