Go Keyword import: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 29: Line 29:
The package path is enclosed in double quotes. It is a [[Go Concepts - Lexical Structure#String_Literal|string literal]]. It is NOT the [[Go Concepts - Packages#Package_Names|package name]], but it helps the compiler to locate the package, instead.
The package path is enclosed in double quotes. It is a [[Go Concepts - Lexical Structure#String_Literal|string literal]]. It is NOT the [[Go Concepts - Packages#Package_Names|package name]], but it helps the compiler to locate the package, instead.


 
The package path represents a local file system path fragment. That path fragment is supposed to live under a <tt>src</tt> directory whose parent is listed in the <tt>[[Go Concepts - Runtime#GOPATH|GOPATH]]</tt> environment variable. Note that if the path has multiple segments, only the last one is the package name and can used to prefix identifiers belonging to the imported package, in the program. For example, if we import <tt>import "project1/blue"</tt>, we use only <tt>blue</tt> to qualify identifiers from that package: <tt> blue.DoSomethingBlue()</tt>
 
 
 
 
It represents a local file system path fragment. That path fragment lives under a <tt>src</tt> directory whose parent is listed in <tt>GOPATH</tt>. For more details on how <tt>GOPATH</tt> works go to <tt>[[Go Concepts - Runtime#GOPATH|GOPATH]]</tt>.
 
Note that if the path has multiple segments, only the last one can used to prefix identifiers in the program. For example, if we import <tt>import "project1/blue"</tt>, we can use only <tt>blue</tt> to qualify identifiers from that package: <tt> blue.DoSomethingBlue()</tt>


The <tt>package-name-alias</tt> is optional, and it should be used when we have a package name conflict - we're trying to use two package whose terminal path segment is the same. If <tt>package-name-alias</tt> is not specified, the package will be referred as the last segment of the path.
The <tt>package-name-alias</tt> is optional, and it should be used when we have a package name conflict - we're trying to use two package whose terminal path segment is the same. If <tt>package-name-alias</tt> is not specified, the package will be referred as the last segment of the path.
The package name - the string literal that is mentioned after the <tt>package</tt> keyword when the package elements are defined and after the <tt>import</tt> keyword when the package elements are imported into another package - is a filesystem path fragment that consists in path elements separated by slashes. In the simplest case, the path fragment contains just one element. The filesystem path fragments represent directories on the local filesystem that live under "src" directories, which in turn live under parent directories that are listed in <tt>[[Go Concepts - Runtime#GOPATH|GOPATH]]</tt>.


=Importing Multiple Packages=
=Importing Multiple Packages=

Revision as of 01:35, 2 April 2016

External

Internal

Overview

An import declaration states that the current package depends on the functionality of the imported package and wants access to its name space. Thus, the current package gains access to the exported identifiers exposed by the imported package.

...
import [package-name-alias] "package-path-string-literal"
...

Example

...
import "net/http"
import "project1/blue"
...

The package path is enclosed in double quotes. It is a string literal. It is NOT the package name, but it helps the compiler to locate the package, instead.

The package path represents a local file system path fragment. That path fragment is supposed to live under a src directory whose parent is listed in the GOPATH environment variable. Note that if the path has multiple segments, only the last one is the package name and can used to prefix identifiers belonging to the imported package, in the program. For example, if we import import "project1/blue", we use only blue to qualify identifiers from that package: blue.DoSomethingBlue()

The package-name-alias is optional, and it should be used when we have a package name conflict - we're trying to use two package whose terminal path segment is the same. If package-name-alias is not specified, the package will be referred as the last segment of the path.

Importing Multiple Packages

import (
    "fmt"
    "strings"
)

Accessing an Identifier from an Imported Package

Once a package has been declared as "imported", all exported identifiers from the imported package are available in the importing package. In order to access an individual exported identifier, the identifier should be prefixed with the name of the package. For example, in order to access the struct Box exposed by package "pkgA", we do this:

import "pkgA"
...
func f() {
    ...
    var b pkgA.Box
    ...
}

How Packages are Resolved

The algorithm is described here:

GOPATH