Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
37 views25 pages

Go Package Naming & Formatting Guide

The document discusses Go programming concepts including using packages, naming and organizing files, capitalization conventions for public and private functions, and using go fmt to format code. It emphasizes that the package name must match the containing folder name except when the package is main, which must contain a function called main.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views25 pages

Go Package Naming & Formatting Guide

The document discusses Go programming concepts including using packages, naming and organizing files, capitalization conventions for public and private functions, and using go fmt to format code. It emphasizes that the package name must match the containing folder name except when the package is main, which must contain a function called main.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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

You might also like