Professional Documents
Culture Documents
Cheatsheet For Packages
Cheatsheet For Packages
You can use package clause in a file to let Go know that which package that file belongs to.
main package
main.go bye.go
IMPORTING
Importing is like as if you've declared what's inside the imported package's files in your own file.
format.go
print.go
errors.go
myfile.go
import "fmt" package
package
import "errors" fmt
errors
import "time"
bye.go time.go
hey.go
package
package time
your
PACKAGE SCOPE
Each Go package has its own scope. For example, declared funcs are only visible to the files belong
to the same package.
package main
import "fmt"
"main()" is visible
throughout func main() {{
the main package fmt.Println( "Hello!")
}}
SCOPE
Every line of code can have different scope depending on their position in a Go file.
package main
file scoped import "fmt"
only visible in this file
const ok = true
package scoped
visible to all the files func main() {{
belong to the package
package main
import "fmt"
func nope() {{
const ok = true block scoped
declaration
var hello = "Hello!"
_ = hello only visible
}} in "nope" func
func main() {{
fmt.Println(hello, ok)
}}
PACKAGE SCOPE
Each Go package has its own scope
For example, declared funcs are visible in the same package
main.go bye.go
main.go
bye.go
package main
func bye() {{
fmt. Println( "Bye!")
}}
package main
import "fmt"
func bye() {{
✓ fmt. Println( "Bye!")
}}
FILE SCOPE
Each file has to import external packages on its own
main package
main.go bye.go
fmt
learngo/first/explain/importing
PACKAGE KINDS
There are two kinds of packages in Go: Executable and Library.
Executable Library
Package Package
package package
main fmt
+
func main( )
LIBRARY EXECUTABLE
created for reusability created for running
non-executable executable
importable non-importable