Go Language Modularization: Difference between revisions
Line 31: | Line 31: | ||
==Declaring Packages== | ==Declaring Packages== | ||
===<span id='main'></span>The <tt>main</tt> Package=== | ===<span id='main'></span>The <tt>main</tt> Package=== | ||
The <code>main</code> package produces an executable as result of its compilation. The <code>main</code> package must have a function called <code>main()</code>, which is where the code execution starts. | The <code>main</code> package produces an executable as result of its compilation. The <code>main</code> package must have a function called <code>main()</code>, which is where the code execution starts. The name of the source code file that declares the <code>main()</code> function does not matter. | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
package main | package main | ||
Line 39: | Line 38: | ||
fmt.Printf("hello\n") | fmt.Printf("hello\n") | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 01:19, 23 August 2023
External
Internal
Overview
A standard organization of the files that are part of a project makes easier to share code with other people who also use the same standard. Go workspaces encourage such a standard.
Overview
Workspaces
The standard workspace layout is:
. ├─ src ├─ pkg └─ bin
This layout is recommended, but not enforced.
A workspace may contain multiple projects.
Define the relationship between workspace and the GOPATH
variable.
Project
Packages
A package is a group of related source files. A package can be imported by other packages. Always, there must be one package called main
, which produces an executable as result of its compilation. Other packages do not produce executables as result of their compilation.
Must the package live in a directory with the same name?
Deplete, merge into this document and delete: Go Concepts - Packages
Declaring Packages
The main Package
The main
package produces an executable as result of its compilation. The main
package must have a function called main()
, which is where the code execution starts. The name of the source code file that declares the main()
function does not matter.
package main
import "fmt"
func main() {
fmt.Printf("hello\n")
}
Importing Packages
When a package is imported during compilation, the compiler searches directories specified by the GOROOT
and GOPATH
environment variables.
import Keyword
The import
is a keyword use to access other packages.
Relationship between Source Code Files and Packages
TODO: how do we add source code files to package? Is it a flat space? It is a hierarchy? What kind of names should the files use?
Standard library
Go comes with a set of "built-in" packages, which come with the Go development environment.
The content of the Standard library is available here: https://pkg.go.dev/std
Interesting Standard library packages:
Dependencies
Vendoring
"Vendoring" is the act of making a local copy of a third party package your project depends on. This copy is traditionally placed inside each project and then saved in the project repository.
TODO
Deplete, merge into this document and delete: