Using Libraries
(aka, using other people’s code)
(aka, using code in other packages)
(aka, using packages)
naming & organizing
files and packages
Package name must match containing folder name …
… unless the package is main
Reminder:
There can only be one main function per executable program.
You can have other functions in the main package.
File names: short, evocative, concise, error on the side of brevity.
You can have as many files
as you’d like in a package
By the way, FYI
Notice the folder structure:
- 02_library is not idiomatic go
- I’m using this non-idiomatic structure to
keep these files in sequence
capitalization
Public / private
(capitalized) / (camelCase)
applies to functions, variables, and types
Reminder:
For a function to be available outside a package,
the function name must be uppercase
Reminder:
For a function to be available outside a package,
the function name must be uppercase
reverseTwo is available inside the
package ...
Reminder:
For a function to be available outside a package,
the function name must be uppercase
ns
ru
de
co
Reminder:
For a function to be available outside a package,
the function name must be uppercase
reverseTwo is available inside the stringutil package …
r un
but not outside the package.
sn’t
e
do
de
co
ing?
tr at
s
on
em
hi sd
t ist
wha
Public function
not public function
ing?
tr at
s
on
em
hi sd
t ist
wha
go fmt
formats your code
nice article on go fmt
My code is not formatted in as idiomatic code.
My code is formatted in as idiomatic code.
Semicolons are automatically inserted by the compiler.
Your curly braces can’t be like the code above.
Semicolons are automatically inserted by the compiler.
Now my curly braces are correct
Review
● package name must match folder name
○ except for package main
● package main
○ must have func main() {}
○ can contain other functions also
● packages can be organized into any number of files
● capitalization
○ Public
○ private
● camelCase
● go fmt
Review Questions
package main
Does package main have to be in a folder
called main?
package main
Does package main have to have a func called
main?
camelCase
True or False:
idiomatic go uses camelCase.
go fmt
● Write “hello go” again, indenting your code all of the way
to the left
○ take a screenshot of this
● run go fmt at the terminal
○ take a screenshot of this