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

huangapple go评论99阅读模式
英文:

Access values of Go struct in external function

问题

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

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\n", err)
    } else {
        release := releases[0]
        fmt.Printf("Version: %+v\n", *release.Name)
    }

}

编辑

我已经修改了函数,使其返回一个字符串(我不确定这样是否正确),但希望它能帮助解释我想要做什么。

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\n", err)
    } else {
        release := releases[0]
    }
    return *release.Name
}

我想要返回*release.Name的值,而不仅仅是打印出来,这样我就可以从另一个函数中访问该值,但是我不太理解在这种情况下返回是如何工作的(对Go语言非常陌生)。

我在想是否可以将结构体作为字符串返回,但是当我运行代码时会出现错误。

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

这让我觉得我可能没有正确处理这个问题。有人可以指点我正确的方向吗?

英文:

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\n", err)
    } else {
        release := releases[0]
        fmt.Printf("Version: %+v\n", *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\n", 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

得分: 1

一个问题在这里:

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

在标记的那一行,你定义了一个新的变量叫做release,它的值等于releases[0],它的作用域仅限于else子句(使用:=)。然后它立即超出了作用域。我很惊讶你没有收到未使用变量的警告。看起来你还需要将release的类型更改为github.RepositoryTag。尝试一下:

var release github.RepositoryTag
...
if err != nil {
...
} else {
    release = releases[0]  // 注意等号
}

然而,更符合惯用方式的做法可能是这样的(未经测试):

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
    }
}
英文:

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
    }
}

huangapple
  • 本文由 发表于 2015年7月25日 10:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/31622282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定