Commit 3aec2e46 authored by Rob Pike's avatar Rob Pike

rewrite the section about imports

DELTA=42  (10 added, 11 deleted, 21 changed)
OCL=35017
CL=35020
parent 0c18d138
...@@ -4176,23 +4176,12 @@ An implementation may require that all source files for a package inhabit the sa ...@@ -4176,23 +4176,12 @@ An implementation may require that all source files for a package inhabit the sa
<h3 id="Import_declarations">Import declarations</h3> <h3 id="Import_declarations">Import declarations</h3>
<p> <p>
An import declaration states that the current package depends on the An import declaration states that the source file containing the
<i>imported</i> package and provides acccess to its declaration uses identifiers
<a href="#Exported_identifiers">exported identifiers</a>. <a href="#Exported_identifiers">exported</a> by the <i>imported</i>
</p> package and enables access to them. The import names an
identifier (PackageName) to be used for access and an ImportPath
<p> that specifies the package to be imported.
The import declaration binds a package name to the imported package (except in
the case of <code>.</code> or <code>_</code> imports; see below). The package name
denotes the imported package within the current source file. If no explicit
package name is present, the package name defined within the source
files of the imported package is used.
</p>
<p>
The imported package is specified by an import path; the meaning of the path
is implementation-dependent. It may be a file name relative to a repository
of installed packages and the file a (compiled) implementation of the package.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
...@@ -4203,22 +4192,30 @@ ImportPath = StringLit . ...@@ -4203,22 +4192,30 @@ ImportPath = StringLit .
</pre> </pre>
<p> <p>
If a package <code>A</code> is imported by a package <code>P</code> and The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
<code>A</code> exports an identifier <code>X</code>, then <code>X</code> to access the exported identifiers of the package within the importing source file.
may be referred to by the <a href="#Qualified_identifiers">qualified identifier</a> It is declared in the <a href="#Blocks">file block</a>.
<code>A.X</code> within <code>P</code>. If an explicit package name If the PackageName is omitted, it defaults to the identifier specified in the
<code>B</code> is present, <code>X</code> must be referred to as <code>B.X</code>. <a href="#Package_clauses">package clause</a> of the imported package.
Finally, if the import declaration uses an explicit period If an explicit period (<code>.</code>) appears instead of a name, all the
(<code>.</code>) for the package name, <code>X</code> will be declared package's exported identifiers will be declared in the current file's
in the current file's <a href="#Blocks">file block</a> and can be accessed file block and can be accessed without a qualifier.
without a qualifier. </p>
<p>
The interpretation of the ImportPath is implementation-dependent but
it is typically a substring of the full file name of the compiled
package and may be relative to a repository of installed packages.
</p> </p>
<p> <p>
In this table, assume we have compiled a package named Assume we have compiled a package containing the package clause
<code>math</code>, which exports function <code>Sin</code>, and <code>package math</code>, which exports function <code>Sin</code>, and
installed the compiled package in file installed the compiled package in the file identified by
<code>"lib/math"</code>. <code>"lib/math"</code>.
This table illustrates how <code>Sin</code> may be accessed in files
that import the package after the
various types of import declaration.
</p> </p>
<pre class="grammar"> <pre class="grammar">
...@@ -4230,6 +4227,8 @@ import . "lib/math" Sin ...@@ -4230,6 +4227,8 @@ import . "lib/math" Sin
</pre> </pre>
<p> <p>
An import declaration declares a dependency relation between
the importing and imported package.
It is illegal for a package to import itself or to import a package without It is illegal for a package to import itself or to import a package without
referring to any of its exported identifiers. To import a package solely for referring to any of its exported identifiers. To import a package solely for
its side-effects (initialization), use the <a href="#Blank_identifier">blank</a> its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment