如何在Go中访问被变量隐藏的命名空间?

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

How to access a namespace hidden by a variable in Go?

问题

我最近用Go语言写了以下代码:

import (
    tasks "code.google.com/p/google-api-go-client/tasks/v1"
)

func tasksMain(client *http.Client, argv []string) {
    taskapi, _ := tasks.New(client)
    tasklists, _ := taskapi.Tasklists.List().Do()
    
    for _, tasklist := range tasklists.Items {
        tasks, _ := taskapi.Tasks.List(tasklist.Id).Do()
        for _, task := range tasks.Items {
            log.Println(task.Id, task.Title)
        }
    }
}

但后来我意识到现在命名空间"tasks"被变量"tasks"隐藏了。

所以我想知道,一旦命名空间被变量隐藏了,是否还有办法访问它?如果没有,是否有其他常见的处理这种情况的技巧?由于Go语言使用了一些奇怪的短命名空间(如"url"、"bytes"、"strings"等),它似乎保留了各种潜在的变量名。有什么建议吗?

英文:

I've recently wrote the following code in Go:

import (
	tasks "code.google.com/p/google-api-go-client/tasks/v1"
)

func tasksMain(client *http.Client, argv []string) {
	taskapi, _ := tasks.New(client)
	tasklists, _ := taskapi.Tasklists.List().Do()
	
	for _, tasklist := range tasklists.Items {
		tasks, _ := taskapi.Tasks.List(tasklist.Id).Do()
		for _, task := range tasks.Items {
			log.Println(task.Id, task.Title)
		}
	}
}

But then I realized that now the namespace "tasks" is hidden by the variable "tasks".

So I'm wondering, is there any way to still access the namespace once it's hidden by a variable? If not, is there any other common technique to handle this situation. With all the strangely short namespaces that Go uses ("url", "bytes", "strings", etc.), it seems it's reserving itself all kind of potential variable names. Any suggestion?

答案1

得分: 6

以下是翻译好的内容:

你可以重命名变量或用于引用包的名称,因此没有什么是“保留”的。

在导入时给包指定另一个别名:

import (
    tsk "code.google.com/p/google-api-go-client/tasks/v1"
)

...

taskapi, _ := tsk.New(client)
英文:

There's nothing "reserved" as you can both rename your variables or the name used to refer to the package.

Simply give another alias to the package when importing :

import (
    tsk "code.google.com/p/google-api-go-client/tasks/v1"
)

...

taskapi, _ := tsk.New(client)

答案2

得分: 2

这与命名空间无关,它是作用域的一般属性:

func foo() {
    var a int
    {
        a := 42
        // 无法在此处访问外部的 'a'。
    }
}

解决方法与一般情况相同:如果你想要访问外部作用域中的实体(外部的那个),就不要定义与外部作用域中已定义的实体同名的实体。在上面的代码片段中,内部的 'a' 可以命名为 'a2'、'b' 或其他任何除了 'a' 以外的名称。

当然,dystroy 的回答也是有效的。它等同于将外部的 'a' 重命名为其他名称。

我只是想指出你的问题是一个特定实例的一般问题,而一般解决方案适用于它。

英文:

It has nothing to do with namespaces. It's a general property of scoping:

func foo() {
        var a int
        {
            a := 42
            // Cannot access the outer 'a' here.
        }
}

And the solution is the same as in the general case: Do not define entities named the same as entities defined in outer scopes if you want to access them (the outer ones). In the above snippet, the inner 'a' would be named eg. 'a2' or 'b' or whatever except 'a'.

dystroy's answer is valid as well, of course. It equals to renaming the outer 'a' to something else.

I'm just trying to point out that your problem is a specific instance of a general issue and the general solution applies to it.

huangapple
  • 本文由 发表于 2013年9月6日 14:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/18651558.html
匿名

发表评论

匿名网友

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

确定