在go中导入包

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 Files.readAllLines etc without having Files imported.

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:
", 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.

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.

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

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.