Go Concepts - Runtime: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
* [[Go Concepts#Subjects|Concepts]]
=Relevance=
Go 1.6
=Environment Variables=
==<tt>GOPATH</tt>==
<tt>GOPATH</tt> is used by the compiler to locate ''source files'' and ''package objects'' for the [[Go Concepts - Packages#Overview|package]]s listed by the [[Go Keyword import#How_Packages_are_Resolved|import]] statements in source files being compiled.
It must be an ''absolute'' path.
<tt>GOPATH</tt> content lists directories on the local file system. <tt>GOPATH</tt> may contain multiple directories. On UNIX, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. <tt>GOPATH</tt> must be set every time we need to get and build packages outside the standard Go tree.
The compiler loops over the values listed in <tt>GOPATH</tt>, in order, appends <tt>/src/<imported-package-name></tt> to them and looks into the directories whose name are thus generated for source files that belong to the required packages. This is why it is important that the package and the directory containing the package sources have the same name: if they don't, the compilation fails to find the source file.
Each directory listed in GOPATH must have a prescribed structure:
* The <tt>src</tt> directory holds source code.  The path below <tt>src</tt> determines the import path or executable name.
* The <tt>pkg</tt> directory holds installed package objects. As in the Go tree, each target operating system and architecture pair has its own subdirectory of <tt>pkg</tt>: <tt>pkg/GOOS_GOARCH</tt>.
If <tt>DIR</tt> is a directory listed in the <tt>GOPATH</tt>, a package with source in <tt>DIR/src/x/y</tt> can be imported as "<tt>x/y</tt>" and has its compiled form installed to "<tt>DIR/pkg/GOOS_GOARCH/x/y.a</tt>".
* The bin directory holds compiled commands. Each command is named for its source directory, but only the final element, not the entire path.  That is, the command with source in <tt>DIR/src/x/blah</tt> is installed into <tt>DIR/bin/blah</tt>, not <tt>DIR/bin/x/blah</tt>.  The "<tt>x/</tt>" prefix is stripped so that you can add <tt>DIR/bin</tt> to your PATH to get at the installed commands. 
===Relative Order===
Order in which root directories are listed in <tt>GOPATH</tt> matters. If two directories contain packages with the same name, the behavior corresponding to the package whose root directory is listed first in the list is used.
==="internal" directories===
Code in or below a directory named "internal" is importable only by code in the directory tree rooted at the parent of "internal". For more details see https://golang.org/s/go14internal.
===Vendor directories===
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[Go Concepts - Dependency Handling#Vendoring|Vendoring]]
</blockquote>
===In-line Documentation===
<pre>
go help gopath
</pre>
==<tt>GOBIN</tt>==
If the GOBIN environment variable is set, commands are installed to the directory it names instead of <tt>DIR/bin</tt>. <tt>GOBIN</tt> must be an absolute path.
==<tt>GOROOT</tt>==
=Executables vs. Libraries=
=Executables vs. Libraries=


Line 60: Line 7:
==Compiling an Executable==
==Compiling an Executable==


According to the specifications (https://golang.org/ref/spec#Program_execution), a complete program is created by linking a single, unimported package called the ''main'' package with all the package it imports, transitively. The main package must be named "main". The main package must declare a [[Go Concepts - Functions#The_main.28.29_Function|function named <tt>main()</tt> that takes no arguments and returns no value]]. The <tt>main()</tt> function is the entry point in the program.  
According to the specifications (https://golang.org/ref/spec#Program_execution), a complete program is created by linking a single, unimported package called the ''main'' package with all the package it imports, transitively. [[Go_Language_Modularization#The_.22main.22_Package|The main package]] must be named "main". The main package must declare a [[Go Concepts - Functions#The_main.28.29_Function|function named <tt>main()</tt> that takes no arguments and returns no value]]. The <tt>main()</tt> function is the entry point in the program. If the "main" package does not contain a <tt>main()</tt> function, the build tool won't produce an executable:
 
<pre>
runtime.main: call to external function main.main
runtime.main: main.main: not defined
runtime.main: undefined: main.main
</pre>


The program execution begins by initializing the main package and invoking the <tt>main()</tt> function. Then the function invocation returns, the program exists. It does not wait for other (non-<tt>main</tt>) goroutines to complete.
The program execution begins by initializing the main package and invoking the <tt>main()</tt> function. Then the function invocation returns, the program exists. It does not wait for other (non-<tt>main</tt>) goroutines to complete.
Line 66: Line 19:
Note that the name of the source file that contains the <tt>main()</tt> function does NOT have to be "main.go". It could be any legal file name. The executable will be created under the name of the file that contains the <tt>main()</tt> function.
Note that the name of the source file that contains the <tt>main()</tt> function does NOT have to be "main.go". It could be any legal file name. The executable will be created under the name of the file that contains the <tt>main()</tt> function.


The simplest executable can be created as follows:
The simplest possible executable named <tt>example</tt> can be created as follows:


Declare an <tt>example.go</tt> that defines the "main" package and contains the <tt>main()</tt> function:
Declare an <tt>example.go</tt> that defines the "main" package and contains the <tt>main()</tt> function:
Line 87: Line 40:


The compiler will create an <tt>example</tt> executable.
The compiler will create an <tt>example</tt> executable.
Also see:
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:
:[[Go Concepts - Packages#The_.22main.22_Package|The "main" Package]]
</blockquote>

Latest revision as of 20:44, 7 September 2023

Executables vs. Libraries

Executables are native code programs that can be run directly from command line.

Libraries are collection of native code that can be used by other programs.

Compiling an Executable

According to the specifications (https://golang.org/ref/spec#Program_execution), a complete program is created by linking a single, unimported package called the main package with all the package it imports, transitively. The main package must be named "main". The main package must declare a function named main() that takes no arguments and returns no value. The main() function is the entry point in the program. If the "main" package does not contain a main() function, the build tool won't produce an executable:

runtime.main: call to external function main.main
runtime.main: main.main: not defined
runtime.main: undefined: main.main

The program execution begins by initializing the main package and invoking the main() function. Then the function invocation returns, the program exists. It does not wait for other (non-main) goroutines to complete.

Note that the name of the source file that contains the main() function does NOT have to be "main.go". It could be any legal file name. The executable will be created under the name of the file that contains the main() function.

The simplest possible executable named example can be created as follows:

Declare an example.go that defines the "main" package and contains the main() function:

package main

import "fmt"

func main() {
    fmt.Println("I am example")
}

Build the executable:

go build ./example.go

The compiler will create an example executable.