Importing packages in go

go modules import local package
golang import
golang packages
golang package structure
golang import package from sub-directory
golang multiple packages in same directory
golang package main
local import in non-local package

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:\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.

Importing Packages in Go, An import statement is made up of the import keyword along with the name of the package. As an example, in the Go program file random.go you  If you import a package but don't use any of it's exported names, the Go compiler will print an error-message. To circumvent this, you can set the alias to the underscore: import (_ "fmt") This can be useful if you don't access this package directly but need it's init functions to run.

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

Understanding Golang Packages – The New Stack, The keyword “import” is used for importing a package into other packages. In the Code Listing -1, we have imported the package “fmt” into the  1. Import each package directly. Go supports the direct import of each of the packages as well as grouped import. Here is the way to do that. import "fmt" // import packages one by one import "os" Here is the grouped import syntax. import ( "fmt" "os" ) 2. Aliased imports in Go

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.

How to Write Go Code, Importing Packages. There are two ways to import packages in Go - // Multiple import statements import "fmt  The import "packagename" statement is used to import an existing package. packagename.FunctionName() is the syntax to call a function in a package. In line no. 3, we import the fmt package to use the Println function. The fmt is a standard package and is available inbuilt as a part of the Go standard library.

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.

A beginners guide to Packages in Golang, Now, in the top-level main.go , you could import "myproject/mylib" and it would work OK. If you are using Go 1.5 above, you can try to use vendoring feature. It allows you to put your local package under vendor folder and import it with shorter path. In your case, you can put your common and routers folder inside vendor folder so it would be like

How to use custom packages, Go Importing packages. Example#. You can import a single package with the statement: import "path  The go.mod file defines the module’s path, which is also the import path used while importing packages that are part of this module. Before Go module got introduced in Go 1.11, every project needed to be created inside the so-called GOPATH. The path of the project inside GOPATH was considered its import path.

Go, Go standard library (stdlib) packages are inside $GOROOT directory and when importing, you don't need to write full path names for stdlib  When you import a package, Go create a variable using the package declaration of the package. If you are importing multiple packages with the same name, this will lead to a conflict. //

How to use Go packages?, Learn how Go packages work and how to import them in your code. This tutorial also deals with creating custom packages using Go modules. import "fmt". fmt is the name of a package that includes a variety of functions related to formatting and output to the screen. Bundling code in this way serves 3 purposes: It reduces the chance of having overlapping names. This keeps our function names short and succinct.

Comments
  • I just tried to do a Files.readAllLines without having Files imported and it didn't compile. I can't find anything that says that the java.nio.file package is automatically imported.
  • the import is ioutil, but the package name is iotuil, is that a typo? Could fix that if it is or clarify it to me please. Thnx
  • The GopherCon 2014 keynote speech by Rob Pike (one of the languages creators) is a really interesting introduction to the thought processes that went in to designing the language. It covers the package system (in short, they did it for readability). Well worth a watch. youtube.com/watch?v=VoS7DsT1rdM
  • It should be noted that the dot-import is not considered good practice. From golang.org: If a program imports a standard package using import . "path", additional names defined in the imported package in future releases may conflict with other names defined in the program. We do not recommend the use of import . outside of tests, and using it may cause a program to fail to compile in future releases.
  • @jimt Awesome, thanks for that (it just felt wrong for some reason, glad that is actually isn't advised :) ). I'll update the answer to reflect that.
  • If golang.org has an objection to import . "path" they should remove it from the language. Why would it be a good thing to use for tests ("outside of tests...")? Aren't tests kept forever these days? And the . import, is exactly what every import statement in Java does. In fact, that is all import statements in Java do. I don't see much java code that isn't doing a ". import".
  • Java isn't Go. The dot-import is in the language for much the same reasons the builtin print and println are still in there (legacy and bootstrapping). They too are not intended for production use and may disappear into thin air at any time.
  • Correct me if I'm wrong, but it shouldn't do any harm if you have project-specific code that you spread across multiple files which you would otherwise have in your main package. But standard- and 3rd party libraries should have the namespace prefix to avoid naming collision.
  • Files in Files.readAllLines is not a package name. It is a class name being used to call a static method. The package for Files is java.nio.file, so without an import java.nio.file.Files; you would have to say: java.nio.file.Files.readAllLines()