Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Go Language Overview!
You have completed Go Language Overview!
Preview
A Go program is split into several libraries, called packages.
- Each file begins with
packagedeclaration- Packages are often used to set up a library for other programs to use. But there's a special package that provides a starting point for Go programs to run: the
mainpackage.
- Packages are often used to set up a library for other programs to use. But there's a special package that provides a starting point for Go programs to run: the
-
importmakes other package contents available for use in the current source file - Package contents
- Variables, functions, and other things declared at package level will be accessible anywhere in the package.
- Again, there's a special starting point for Go programs to run: the
mainfunction.
- Using imported packages
- To refer to names of variables/constants/functions in another package, need to qualify the name:
fmt.Println,welcome.English. - No need to qualify names declared in current package
- To refer to names of variables/constants/functions in another package, need to qualify the name:
package main
// We'll be calling functions from these two packages.
// So we need to import them here.
// Note the parentheses: this syntax lets you import
// multiple packages at once.
import (
"fmt"
"math"
)
// Declare a variable that will be accessible throughout
// the current package.
var myNumber = 1.23
func main() {
// Call the Ceil function from the "math" package.
// Then, assign it to a variable. This is the "short
// variable declaration" syntax, which we'll talk
// about in the "Variables" video.
roundedUp := math.Ceil(myNumber)
// Call the Floor function from the "math" package.
roundedDown := math.Floor(myNumber)
// Call the Println function from the "fmt" package.
fmt.Println(roundedUp, roundedDown)
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Packages are often use to set up
a library for other programs to use.
0:00
But there's a special package that
provides a starting program for
0:03
Go programs to run, the main package.
0:05
We see this here at the start
of our hello Go sample program.
0:08
Following the package declaration,
each go file includes an import statement,
0:12
which states the names of several other
packages that it needs to import.
0:16
Once you've imported a package, you can
use functions, constants, variables, and
0:21
other things of declares
within your current file.
0:26
Then following the import statement
comes the actual package contents.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up