Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Here's how to organize your source code into packages. We'll also show you how to download and install new packages automatically, using the "go get" tool.
Your OS's $GOPATH
environment variable should be set to a directory where you will keep all your Go packages. Once that's done, from your console/terminal, run this command:
go get github.com/treehouse-projects/go-intro/welcome
That will visit this GitHub project and download a file with the below contents at $GOPATH/src/github.com/treehouse-projects/go-intro/welcome/welcome.go
:
package welcome
const English = "Welcome"
const Japanese = "ようこそ"
// Below languages not yet approved by committee
const klingon = "yI'el"
Once you've set up the welcome
package, you can use the package by creating another file named print_welcome.go
in the current directory with the following contents:
package main
import (
"fmt"
"github.com/treehouse-projects/go-intro/welcome"
)
func main() {
fmt.Println(welcome.English)
fmt.Println(welcome.Japanese)
}
Downloading a package from the Go team
The Go team has made this example package available on GitHub. You can download and install it by running:
go get github.com/golang/example/stringutil
Once you've done so, you can use the package via the following code:
package main
import (
"fmt"
"github.com/golang/example/stringutil"
)
func main() {
fmt.Println(stringutil.Reverse("hello"))
}
-
0:01
In addition to the package name, Go programs that want to import your package
-
0:04
will need to know its package path.
-
0:06
The package path is decided by what directory you store your package code in.
-
0:10
The Go workspace directory is a folder on your computer where all of
-
0:13
your Go code should be stored.
-
0:15
The Go workspace is not the same thing as Treehouse workspaces.
-
0:19
Sorry if that's a little confusing.
-
0:21
By default, the Go workspace will be a go sub directory in your user's
-
0:25
home directory, but you can change this using the GOPATH environment variable.
-
0:32
So let's take a look what GOPATH is set to here in this workspace.
-
0:35
Here in treehouse/workspaces,
-
0:37
GOPATH is setup to point to the workspace subdirectory.
-
0:41
Go code should go in a source subdirectory within the workspace, and
-
0:44
then you should create a subdirectory for the each segment of the package path.
-
0:47
We've set this package under github.com/treehouse-projects/go-intro and
-
0:54
then the welcome package.
-
0:56
We'll explain why we use such a detailed package path shortly.
-
1:00
Let's create a Go program that uses this package.
-
1:03
To import a package into a go file,
-
1:05
you'll use the package path, followed by the package name.
-
1:08
So let's go here into our temp.go file, and as always, this is going
-
1:13
to run from the command line, so we're going to put this in the main package.
-
1:17
And then we'll do an import statement.
-
1:18
We're gonna import a couple different packages here.
-
1:23
So we'll use the multi-package form of the import command.
-
1:29
First we'll import the format packet so that we can use the print line function.
-
1:34
And then we'll import our welcome package.
-
1:38
To do that, we're going to leave the source directory off,
-
1:41
that's always assumed by default.
-
1:43
But we'll start with
-
1:44
github.com/treehouse-projects/go-intro/
-
1:55
and that's the import path, and now we need the name of the actual package.
-
2:01
That'll be the last segment of our import string.
-
2:07
So we'll use the welcome package name.
-
2:11
I never actually used the contents that we imported from that package, so
-
2:15
we'll create a main function here which will run when our program does.
-
2:20
And we're gonna call the Println function, and
-
2:24
we'll just pass it some of the constants from the welcome package.
-
2:31
So since these constants are coming from another package,
-
2:35
we need to qualify it using the package name, so we'll say welcome.,
-
2:40
and then we'll say the name of our English constant.
-
2:44
Remember, that starts with a capital letter because it's exported.
-
2:48
And then we'll just copy this and, Print out the Japanese welcome as well.
-
2:59
Save that, now let's try running it.
-
3:02
go run temp.go,
-
3:05
and it prints out our two welcomes, the first in English, second in Japanese.
-
3:11
Now, what if we were to try to use the unexported constant though?
-
3:16
So, let's try adding a third Println call.
-
3:21
And this time, we're going to try to access the welcome packages
-
3:26
klingon, Constant.
-
3:30
But remember, this is an unexported constant,
-
3:33
because it begins with a lowercase letter.
-
3:36
Let's try running this. You see, this time it refuses to compile.
-
3:41
It can't refer to an unexported name, welcome.klingon.
-
3:45
That means that the klingon constant can only be used within the welcome package.
-
3:49
It can't be used from outside it.
-
3:52
We can also download packages from the web and install them on our system for
-
3:56
our go programs to use.
-
3:57
So for example, the golang team here has an example
-
4:02
repository which includes a stringutil package that our programs can use.
-
4:07
So here within the stringutil package I can look in the reverse.go source file and
-
4:14
see that there's a reverse function that I'd like to try using in a package.
-
4:18
So let's try writing a program that uses that.
-
4:21
So I'm gonna import that package,
-
4:26
which has a package path of
-
4:30
github.com/golang/example/stringutil.
-
4:38
stringutil is the package name, and
-
4:39
everything preceding that is the package path.
-
4:43
Once I've imported that package, then I no longer need to use the full package path,
-
4:48
I can just say string, I can just use the package name, stringutil.Reverse.
-
4:58
And I pass it the string that I'd like it to reverse for me.
-
5:03
So the return value of this would be the string that I passed to it backwards.
-
5:08
Let's just print that result out.
-
5:10
fmt.Println.
-
5:14
Hello reversed.
-
5:16
Now if we try to go run temp.go right now, we'll get an error.
-
5:21
Cannot find packet, github.com/golang/example/stringutil.
-
5:26
So first, we'll use the go command's get subcommand to download and
-
5:30
install the package we need.
-
5:32
So I'll just copy and paste the package path and
-
5:35
package name right out of my code down here into the console.
-
5:39
And I'll run the get subcommand of the go command and paste that right here.
-
5:49
The go get command sees that the library package path starts with github.com.
-
5:55
That has no meaning to the go language, but go get knows that GitHub is a site
-
6:00
that hosts git repositories that it can clone the package code from.
-
6:04
So go get downloads the package from GitHub for us.
-
6:07
Now let me refresh our file tree so
-
6:10
that I can show you what changes go get made over here.
-
6:13
go get will create a source subdirectory in your Go workspace directory,
-
6:17
if it doesn't already exist.
-
6:19
And then it'll create subdirectories for each segment of the package path.
-
6:22
So github.com/golang/example/ and
-
6:28
then the stringutil package.
-
6:32
The github.com/go/example repo also contains several other packages.
-
6:37
And go get just clones the whole repo at once.
-
6:39
Now that the stringutil package is installed, we can try rerunning temp.go.
-
6:44
So we'll go back to our console.
-
6:45
And type go run temp.go.
-
6:52
And this time the package will be found.
-
6:54
The program will run successfully, and we'll see our reverse string as output.
You need to sign up for Treehouse in order to download course files.
Sign up