0% found this document useful (0 votes)
16 views

05+Packages+&&+Docs+(code+organization)+Golang

Uploaded by

eowug
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

05+Packages+&&+Docs+(code+organization)+Golang

Uploaded by

eowug
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Packages && Docs

unique namespaces
You can think of “packages” as just another name for “folders.”

Just as a folder can contain more folders and files, so too a


package can contain more packages and files.

The unique name of a “package” is the path after the “src” folder
- this will make more sense in a moment

I do it like this for sequentially storing code. This is good for


teaching or storing code examples that build from step-to-step.
Look at the Go source code
(software development kit, aka, SDK)
to see how it is organized using packages.
Here is more from the Go source code - see how it is organized
using packages.

Remember, you can think of “packages” as just another name


for “folders.”

Just as a folder can contain more folders and files, so too a


package can contain more packages and files.

PACKAGES = FOLDERS
Godoc.org documents
the go language.

Notice how the URL


corresponds with the
package being
documented.
Godoc.org documents
the go language.

Notice how the URL


corresponds with the
package being
documented.
What is this package?

What would be the URL by


which you would look it up at
godoc.org?
What is this package?

What would be the URL by


which you would look it up at
godoc.org?
It’s inspiring to explore the Go source code and speculate as to
what is possible ...
… and then go look at the
docs to see what can be
done.
The unique package name
is determined by the path
after the “src” folder.
Here is the same source code in finder.
Same thing: the unique package
name is the folder path after the
“src” folder
Here is some code from my workspace.

What is the name of this package?


Here is some code from my workspace.

What is the name of this package?

Everything after the “src” folder, remember?


Here is some code from my workspace.

What is the name of this package?

Everything after the “src” folder, remember?

This is Golang’s way of “name spacing”


namespace
In computing, a namespace is used to organize objects of various kinds, so that these objects may be referred
to by name.

Examples include:
● file systems are namespaces that assign names to files
● programming languages organize variables and subroutines in namespaces
● computer networks and distributed systems assign names to resources, such as computers, printers,
websites, (remote) files, etc.

Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts.

Example: human names. Jane Doe. Within the namespace of the Doe family, just "Jane" suffices to
unambiguously designate this person, while within the "global" namespace of all people, the full name must be
used.

In a similar way, hierarchical file systems organize files in directories. Each directory is a separate namespace,
so that the directories may both contain a file "jane". In computer programming, namespaces are typically
employed for the purpose of avoiding name collisions between multiple identifiers that share the same name.
source: wikipedia
Given this namespace / package name ...
We can find documentation
about that code on
godoc.org

Check out the URL.

Notice the import statement


that we would use to use
the code in our code.
Remember how we organize our code?

Remember “go get”?

go get github.com/goestoeleven/golangtraining

It’s all connected.


documentation
godoc.org vs golang.org vs. godoc at terminal

I mainly use godoc.org as it includes the packages of others.


● Go documentation can also be found at golang.org
● Golang.org only has documentation of go source code
○ you won’t find “github.com/julienschmidt/httprouter” here
● Godoc.org has documentation of all go packages, including go source code
○ you will find “github.com/julienschmidt/httprouter” here

● I use godoc.org
package naming
https://golang.org/doc/code.html#PackageNames
package naming
https://golang.org/doc/effective_go.html#names
searching for packages / libraries
search godoc.org
I use this one
Review
● packages
○ packages = folders
● SDK - software development kit
● namespace
○ unique namespace of packages: everything after the “src” folder
● documentation
○ godoc.org
○ golang.org
○ godoc at terminal
● package names / paths
● searching for packages
Review Review - You’ve learned a lot
● golang is awesome ● func main()
● SHA1 ● packages
● go version ● functions vs methods
● go env ● parameters vs arguments
● go help ● expressions vs statements
● environment variables ● variable, constant, literal
○ GOPATH ● go run
○ GOROOT ● go build
● workspace ● go install
○ bin ● go get
○ pkg ○ go commands
○ src ● Go, Github, & Webstorm
■ github.com ● git
● your_user_name ○ git log
○ your_packages ○ .gitignore
■ your code ● Packages / Libraries
● .bash_profile / .bashrc ○ naming
● GO IDE’s ● namespace
○ webstorm ● documentation
○ atom ○ godoc.org
○ golang.org
○ godoc at terminal
Review Questions
Namespacing
Define namespace.
go commands
List and define the go commands you have
learned so far.

I know this is repetitive, but repetition helps us learn.


godoc.org
● Find a package for gorilla sessions on godoc.org
○ take a screenshot of this
namespace
● Open up finder (or windows explorer, for those on
windows).
● Navigate to your workspace.
● Navigate to the folder you created to store the code you
write in this course.
● What is the unique name for this package?
○ for your homework submission
■ write out the unique name for this package and also
■ take a screenshot of finder showing your package

You might also like