Go Language Modularization: Difference between revisions
Line 91: | Line 91: | ||
| <font face='menlo' size='-2'><span id='archive'></span>[[Go Package archive|archive]]</font> || <font face='menlo' size='-2'><span id='atomic'></span>[[go Package atomic|atomic]]</font> || <font face='menlo' size='-2'><span id='bytes'></span>[[go Package bytes|bytes]]</font> || <font face='menlo' size='-2'><span id='container'></span>[[go Package container|container]]</font> || <font face='menlo' size='-2'><span id='database'></span>[[go Package database|database]]</font> || <font face='menlo' size='-2'><span id='encoding'></span>[[go Package encoding|encoding]]</font> || <font face='menlo' size='-2'><span id='errors'></span>[[Go Package errors|errors]]</font> | | <font face='menlo' size='-2'><span id='archive'></span>[[Go Package archive|archive]]</font> || <font face='menlo' size='-2'><span id='atomic'></span>[[go Package atomic|atomic]]</font> || <font face='menlo' size='-2'><span id='bytes'></span>[[go Package bytes|bytes]]</font> || <font face='menlo' size='-2'><span id='container'></span>[[go Package container|container]]</font> || <font face='menlo' size='-2'><span id='database'></span>[[go Package database|database]]</font> || <font face='menlo' size='-2'><span id='encoding'></span>[[go Package encoding|encoding]]</font> || <font face='menlo' size='-2'><span id='errors'></span>[[Go Package errors|errors]]</font> | ||
|- | |- | ||
| <font face='menlo' size='-2'><span id=''></span>[[ | | <font face='menlo' size='-2'><span id='flag'></span>[[go Package flag|flag]]</font> || <font face='menlo' size='-2'><span id='fmt'></span>[[go Package fmt|fmt]]</font> || <font face='menlo' size='-2'><span id='io'></span>[[go Package io|io]]</font> || <font face='menlo' size='-2'><span id='ioutil'></span>[[Go Package ioutil|ioutil]] || <font face='menlo' size='-2'><span id='json'></span><span id='encoding_json'></span>[[Go Package encoding/json|encoding/json]] || <font face='menlo' size='-2'><span id='hash'></span>[[go Package hash|hash]]</font> || <font face='menlo' size='-2'><span id='log'></span>[[go Package log|log]]</font> | ||
|- | |- | ||
| <font face='menlo' size='-2'><span id=''></span>[[ | | <font face='menlo' size='-2'><span id='math'></span>[[go Package math|math]]</font> || <font face='menlo' size='-2'><span id='net'></span>[[Go Package net|net]] || <font face='menlo' size='-2'><span id='os'></span>[[Go Package os|os]]</font> || <font face='menlo' size='-2'><span id='path'></span>[[go Package path|path]]</font> || <font face='menlo' size='-2'><span id='reflect'></span>[[Go Package reflect|reflect]]</font> || <font face='menlo' size='-2'><span id='regexp'></span> [[go Package regexp|regexp]]</font> || <font face='menlo' size='-2'><span id='runtime'></span>[[go Package runtime|runtime]]</font> | ||
|- | |- | ||
| <font face='menlo' size='-2'><span id=''></span>[[ | | <font face='menlo' size='-2'><span id='slices'></span>[[Go Package slices|slices]]</font> || <font face='menlo' size='-2'><span id='sort'></span>[[Go Package sort|sort]]</font> || <font face='menlo' size='-2'><span id='strings'></span>[[go Package strings|strings]]</font> || <font face='menlo' size='-2'><span id='strconv'></span>[[Go Package strconv|strconv]]</font> || <font face='menlo' size='-2'><span id='sync'></span>[[Go Package sync|sync]]</font> || <font face='menlo' size='-2'><span id='text-template'></span>[[go Package text/template|text/template]]</font> || <font face='menlo' size='-2'><span id='time'></span>[[Go Package time|time]]</font> | ||
|- | |- | ||
| <font face='menlo' size='-2'><span id=''></span>[[ | | <font face='menlo' size='-2'><span id='unicode'></span>[[Go Package unicode|unicode]] || || || || || || | ||
|- | |- | ||
|} | |} |
Revision as of 22:11, 6 September 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
Idiomatic Package Conventions
Package names are generally given lowercase, single-word names. They should be short, concise and evocative, and it should provide context for their contents.
Package-exported names start with an uppercase character.
The contents of a package should be consistent with the name. If you start noticing that a package includes extra logic that has no relationship to the package name, consider exporting it as a separate one, or use a more descriptive package name.
Every package should have a comment describing its contents.
Also see:
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. For more details see:
The name of the source code file that declares the main()
function does not matter. The following code, declared in a blue.go
source file, placed in any directory, not necessarily in a main
directory, produces an executable named blue
:
package main
import "fmt"
func main() {
fmt.Printf("hello\n")
}
go build ./blue.go
./blue
hello
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.
import "fmt"
import (
"bufio"
"fmt"
"os"
"strings"
)
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
The standard library is a good source of code examples, comments and style.
Standard library packages:
TO DEPLETE
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.
Export
The functions and variable that start with a capital letter in a package get exported: somebody who imports the package can access the exported names, but not the "private" names, which start with lower case letter. "Hidden" names is also used in this situation. This is how encapsulation is implemented for packages.
In the following example:
package colors
var color string = "blue"
func GetColor() {
return color
}
the color
variable is private to the package, but it can be accessed with the public GetColor()
function.
Modules
Explain go.mod
TODO
Deplete, merge into this document and delete: