0% found this document useful (0 votes)
33 views13 pages

Cheatsheet For Packages

This document discusses Go packages, including how to define a package with a package clause, import packages, and how scoping works for packages, files, and code blocks in Go. Each package has its own scope, files must import any external packages they need, and code within blocks is only visible within that block. There are also two main kinds of packages - executable packages which must be named main and contain a main function for running, and library packages which are importable by other packages but not directly executable.
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)
33 views13 pages

Cheatsheet For Packages

This document discusses Go packages, including how to define a package with a package clause, import packages, and how scoping works for packages, files, and code blocks in Go. Each package has its own scope, files must import any external packages they need, and code within blocks is only visible within that block. There are also two main kinds of packages - executable packages which must be named main and contain a main function for running, and library packages which are importable by other packages but not directly executable.
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/ 13

PACKAGE CLAUSE

You can use package clause in a file to let Go know that which package that file belongs to.

package clause package main


this should be the first code import "fmt"
it can only appear once
func main() {{
fmt.Println( "Hello!")
}}
PACKAGE SCOPE
Declarations which are outside of functions are visible to the files belong to the same package.

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

other packages can't var hello = "Hello!" block scoped


see them fmt.Println(hello, ok) declaration
only visible
}} after its declaration
until " } "
SCOPE
Every line of code can have different scope depending on their position in a Go file.

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

package main package main


import "fmt" import "fmt"
func main() {{ func bye() {{
fmt.Println("Hello!") fmt.Println("Bye!")
bye() }}
}}
FILE SCOPE
Each Go file has its own scope
Imported packages are only visible to the importing file

main.go

"fmt" is visible package main


throughout import "fmt"
the file
func main() {{
fmt.Println( "Hello!")
}}
FILE SCOPE
Each Go file has its own scope
Imported packages are only visible to the importing file

bye.go

package main

func bye() {{
fmt. Println( "Bye!")
}}

bye.go can't use a package that it didn't import


FILE SCOPE
Each Go file has its own scope
Imported packages are only visible to the importing file

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

can have any name name should be main

no func main func main

You might also like