Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A Go program is split into several libraries, called packages.
- Each file begins with
package
declaration- 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
main
package.
- 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
-
import
makes 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
main
function.
- 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
A Go program is split into several
libraries called packages.
0:00
Each file begins with
a package declaration.
0:05
Packages are often use to set up
a library for other programs to use.
0:08
But there's a special package that
provides a starting program for
0:12
Go programs to run, the main package.
0:14
We see this here at the start
of our hello Go sample program.
0:17
Following the package declaration,
each go file includes an import statement,
0:21
which states the names of several other
packages that it needs to import.
0:25
Once you've imported a package, you can
use functions, constants, variables, and
0:30
other things of declares
within your current file.
0:35
Then following the import statement
comes the actual package contents.
0:38
This consists of variables, functions, and
0:41
various other things you want
your package to include.
0:44
Anything that you declare
up here at package level
0:47
will be accessible anywhere
else within this package.
0:49
Again, if you're writing a Go program
that's going to be run from a command
0:53
line, there's a special starting point,
the main function Basically when a go
0:56
program starts running, it looks for
a function named main and invokes that.
1:00
To refer to the names of variables,
constants or
1:05
functions that we've imported from another
package we need to qualify the name.
1:07
So for example this Println function
was imported from the format package.
1:11
To use it, we need to qualify it by
placing the format package name before it,
1:16
so we say fmt followed by a dot And
1:21
then the name of the function that we
want to call from the other package.
1:24
If we were to write a function
elsewhere in this package though.
1:28
Let's say a function named otherFunction.
1:31
And we wanted to call this from here
within our main function we wouldn't need
1:36
to qualify that name since
it's here in the same package.
1:40
We can just say otherFunction.
1:43
Let's try creating another Go program
that uses a couple of different packages.
1:47
Since this is a program that is meant to
be run directly from the command line
1:51
we're going to put it in
the main package again.
1:54
And now I'm going to use a slightly
different form of the import statement so
1:56
that we can import two different packages.
2:02
You'll notice that I put parentheses
here at the end of the import line and,
2:06
I'm going to include the couple different
lines here with the names of a couple
2:11
different packages I want import.
2:15
So as before, we're going to
import the format package so
2:17
that we can use its print line function.
2:20
And then in addition to that,
I'm going to import the math package so
2:23
that we can use a couple
functions from that.
2:27
Now up here, outside of any functions
we're working at the package level.
2:30
I'm going to declare a variable here.
2:34
We'll talk more about
variables in a little bit, but
2:35
any variable that I declare here
is going to be visible within
2:39
any function in the package,
here in the main path.
2:43
So I'm going to declare a variable
named myNumber and I'll just assign it
2:46
the initial value of 1.23 Now let's
create a main function again.
2:51
Remember this will be kicked off
as soon as our program is run.
2:57
I'll create a main function and
3:03
let's call a couple of
functions from the math packet.
3:06
I'm going to call
the ceiling function first.
3:10
Math, remember we need to qualify
the function name with the package name
3:12
since we're getting this
from another package.
3:17
And then I'll call the ceiling function,
ceil, that's an abbreviation.
3:21
And, as I mentioned, the my number
variable was declared a package level, so
3:26
we can access it from here
within the main function.
3:30
So, I'm going to type my number.
3:34
And that'll round my number
up to the next whole number.
3:39
I'm then going to take
the result of that and
3:42
I'm going to assign that to
another variable called roundedUp.
3:45
Now since this variable was
defined within a function,
3:49
it's going to be accessible only
here within the main function.
3:53
We'll talk more about variable
scope in a little bit.
3:58
Then lets call another function
from the math package.
4:01
I'll call math dot floor, which rounds the
number down to the nearest whole number.
4:03
And we'll assign the results back to
another variable named rounded down.
4:10
Then we will just print
both of those numbers out.
4:17
So we'll call the format packages,
print line, function,
4:20
and we'll pass it,
round it up, and round it down.
4:25
Save that.
4:34
Let's try running it real quick.
4:34
Go run temp.go, which I've named my file.
4:37
And it prints out 2 1.
4:42
The first value here is
the result of rounding
4:43
1.23 up to the nearest whole number.
4:48
And the second value here is the result
of rounding it down to the nearest whole
4:52
number.
4:57
Unlike in some other languages,
4:58
it's actually compile error to import
a package and then not use it.
5:00
In other languages, large programs tended
to accumulate many unused library imports
5:04
which slowed compile times.
5:08
So Go doesn't allow that in order
to help your development team
5:10
keep your apps compilation fast.
5:13
So let's try importing another
package into the program here.
5:16
And then we won't actually use it.
5:20
So up here in the import statement
I'm going to add the time package.
5:21
Now let's try running it,
down here on the console.
5:26
And we'll actually see
a compile error this time.
5:30
Imported and not used, time.
5:32
So as I say, make sure to only import
packages that you're going to actually use
5:35
in your program.
5:39
If we remove that again and
rerun our program it should run this time.
5:40
To define a new package
you simply create one or
5:45
more .go files within a single directory.
5:48
Each file needs to begin with
the package declaration,
5:51
which includes the name of the package
a package name should be all lower case.
5:54
And by convention it should
be a single word if possible.
5:58
Then within the package body you declare
one or more constants, variables, or
6:02
functions that you want
your package to include.
6:06
Constants variables or functions that are
named with a capital letter are exported
6:10
from your package and that means
they'll be visible to other packages.
6:14
By contrast, if a constant variable or
function is named with a lower case,
6:18
if it begins with a lower case letter,
it'll be unexported.
6:23
And that means that you can't
use it from other packages.
6:26
So these two constants up here,
their names begin with capital letters,
6:30
that means they'll be
visible from other packages.
6:33
And this one down here,
that's not ready for public use, so
6:35
it's named with a lower case letter.
6:38
And that means it'll be unexported.
6:40
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