在Go语言中,如何直接导入函数,而不需要在调用时加上包名前缀?

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

In Go, how to import function directly, without need to prefix with the package name when I call it?

问题

在Go编程语言中,为什么在导入一个包之后,我仍然需要在该包中的方法前加上包名的前缀?

例如:

import "io/ioutil"

func main() { 
    content, err = iotuil.ReadFile("somefile.txt")
    // 等等..
}

这不是多余的吗?例如在Java中,你可以像导入Files.readAllLines等一样,而不需要导入Files

英文:

In the Go programming language, why after importing a package do I still have to prefix a method within that package with the package name?

i.e.

import "io/ioutil"

func main() { 
    content, err = iotuil.ReadFile("somefile.txt")
    // etc..
}

Isn't this redundant? In Java, for example, you can do things like importing Files.readAllLines etc without having Files imported.

答案1

得分: 70

我猜这并不真正回答你的问题,但如果你想的话,你实际上可以在不明确指定包的情况下调用方法 - 只需在名称前面加上 . 导入(但这不被推荐;请参见下文):

package main

import (
  . "fmt"
  . "io/ioutil"
)

func main () {
  content, err := ReadFile("testfile")
  if err != nil {
    Println("Errors")
  }
  Println("My file:\n", string(content))
}

请注意下面 @jimt 的评论 - 这种做法建议在测试之外使用,因为它可能会导致与未来版本的名称冲突。此外,我完全同意 @DavidGrayson 的观点,即更好地阅读/查看事物的来源。

英文:

I guess this doesn't really answer your question, but if you want, you can actually call the methods without explicitly stating the package - just import with a . in front of the names (but this is not recommended; see below):

package main

import (
  . "fmt"
  . "io/ioutil"
)

func main () {
  content, err := ReadFile("testfile")
  if err != nil {
    Println("Errors")
  }
  Println("My file:\n", string(content))
}

Note @jimt's comment below - this practice is not advised outside of tests as it could cause name conflicts with future releases. Also, definitely agree with @DavidGrayson's point of being nicer to read/see where things come from.

答案2

得分: 55

你可以导入并重命名包名,例如:

    import (  
        .     "fmt"       // 没有名称,在作用域中导入  
        File  "io/ioutil" // 将ioutil重命名为File
        _     "net"       // net将不可用,但net包中的init()函数将被执行
    )

参见 https://golang.org/ref/spec#Import_declarations

英文:

you can import and rename the package name, eg:

    import (  
        .     "fmt"       // no name, import in scope  
        File  "io/ioutil" // rename ioutil to File
        _     "net"       // net will not be available, but init() inside net package will be executed
    )

See also https://golang.org/ref/spec#Import_declarations

答案3

得分: 9

我不能真正代表Go语言的设计者说话,但能够快速确定你调用的方法在哪里定义是很好的。在文件顶部看到你使用的所有包的列表也是很好的。这并不是多余的。

正如你所说,Java要求你写Files.readAllLines,类似地,Go要求你写ioutil.ReadFile

英文:

I can't really speak for the designers of the Go language, but it is nice to be able to quickly tell where the method you are calling is defined. It is also nice to see a list of all the packages your are using at the top of the file. This is not redundant.

As you said, Java requires you to say Files.readAllLines and similarly go requires you to write ioutil.ReadFile.

答案4

得分: 1

在Go语言中,导入语句与Java不同,更像是C++中的#include。在Go中,如果某个东西没有被导入,就无法使用它。如果已经导入了,可以使用它,但必须在前面加上包名作为前缀。正如其他人所说,使用import . "packagename"来导入一个包,就不需要加前缀了。

英文:

Import statements in go are not like in java, more like #include in c++. In go, if something isn't imported, you can't use it. If it is imported, you can use it but must be prefixed with the package name. As everyone else said, use import . "packagename" to import a package and not have to prefix.

huangapple
  • 本文由 发表于 2012年10月17日 08:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/12925450.html
匿名

发表评论

匿名网友

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

确定