q Why it should be named in a specific way?
#Q What if I’d want to rename a package? Any side effects?
#Q How to import a package? I copied a path from the registry, but it didn’t work
#Q Why does import "rsc.io/quote"
and "rsc.io/quote/v4"
both import quote
into the namespace? Any way to differentiate between them?
Every Go program is made up of packages.
Within a package code can refer to any identifier (name) defined within. Clients of the package may only refer the package’s exported names with the prefix that is a name of the package, e.g. time.Now()
.
The program starts running in package main
.
By convention, the package name is the same as the last element of the import path. For instance, the math/rand
package comprises files that begin with the statement package rand
.
Naming
Good package name is short and clear.
- lowercase
- one word, simple noun
- may use abbreviations if they are familiar, common, e.g.
syscall
,fmt
,strconv
- doesn’t steal good variable name from the user, e.g. use
bufio
instead ofbuf
, becausebuf
is a good var name
Examples of good package names:
time
(provides functionality for measuring and displaying time)list
(implements a doubly linked list)http
(provides HTTP client and server implementations)
Naming package contents
In Go package name and its contents’ names are couple since client will use them together.
Avoid repetition. Don’t duplicate package name in the package content, e.g.: