1 00:00:01,150 --> 00:00:04,870 In addition to the package name, Go programs that want to import your package 2 00:00:04,870 --> 00:00:06,500 will need to know its package path. 3 00:00:06,500 --> 00:00:10,850 The package path is decided by what directory you store your package code in. 4 00:00:10,850 --> 00:00:13,910 The Go workspace directory is a folder on your computer where all of 5 00:00:13,910 --> 00:00:15,790 your Go code should be stored. 6 00:00:15,790 --> 00:00:19,350 The Go workspace is not the same thing as Treehouse workspaces. 7 00:00:19,350 --> 00:00:21,490 Sorry if that's a little confusing. 8 00:00:21,490 --> 00:00:25,260 By default, the Go workspace will be a go sub directory in your user's 9 00:00:25,260 --> 00:00:30,020 home directory, but you can change this using the GOPATH environment variable. 10 00:00:32,050 --> 00:00:35,800 So let's take a look what GOPATH is set to here in this workspace. 11 00:00:35,800 --> 00:00:37,400 Here in treehouse/workspaces, 12 00:00:37,400 --> 00:00:41,032 GOPATH is setup to point to the workspace subdirectory. 13 00:00:41,032 --> 00:00:44,740 Go code should go in a source subdirectory within the workspace, and 14 00:00:44,740 --> 00:00:47,905 then you should create a subdirectory for the each segment of the package path. 15 00:00:47,905 --> 00:00:54,550 We've set this package under github.com/treehouse-projects/go-intro and 16 00:00:54,550 --> 00:00:56,380 then the welcome package. 17 00:00:56,380 --> 00:01:00,120 We'll explain why we use such a detailed package path shortly. 18 00:01:00,120 --> 00:01:03,300 Let's create a Go program that uses this package. 19 00:01:03,300 --> 00:01:05,345 To import a package into a go file, 20 00:01:05,345 --> 00:01:08,230 you'll use the package path, followed by the package name. 21 00:01:08,230 --> 00:01:13,470 So let's go here into our temp.go file, and as always, this is going 22 00:01:13,470 --> 00:01:17,110 to run from the command line, so we're going to put this in the main package. 23 00:01:17,110 --> 00:01:18,800 And then we'll do an import statement. 24 00:01:18,800 --> 00:01:23,110 We're gonna import a couple different packages here. 25 00:01:23,110 --> 00:01:29,000 So we'll use the multi-package form of the import command. 26 00:01:29,000 --> 00:01:34,240 First we'll import the format packet so that we can use the print line function. 27 00:01:34,240 --> 00:01:37,340 And then we'll import our welcome package. 28 00:01:38,350 --> 00:01:41,116 To do that, we're going to leave the source directory off, 29 00:01:41,116 --> 00:01:43,140 that's always assumed by default. 30 00:01:43,140 --> 00:01:44,038 But we'll start with 31 00:01:44,038 --> 00:01:54,038 github.com/treehouse-projects/go-intro/ 32 00:01:55,441 --> 00:02:01,980 and that's the import path, and now we need the name of the actual package. 33 00:02:01,980 --> 00:02:05,380 That'll be the last segment of our import string. 34 00:02:07,590 --> 00:02:10,170 So we'll use the welcome package name. 35 00:02:11,840 --> 00:02:15,530 I never actually used the contents that we imported from that package, so 36 00:02:15,530 --> 00:02:20,567 we'll create a main function here which will run when our program does. 37 00:02:20,567 --> 00:02:24,954 And we're gonna call the Println function, and 38 00:02:24,954 --> 00:02:31,061 we'll just pass it some of the constants from the welcome package. 39 00:02:31,061 --> 00:02:35,391 So since these constants are coming from another package, 40 00:02:35,391 --> 00:02:40,585 we need to qualify it using the package name, so we'll say welcome., 41 00:02:40,585 --> 00:02:44,500 and then we'll say the name of our English constant. 42 00:02:44,500 --> 00:02:47,830 Remember, that starts with a capital letter because it's exported. 43 00:02:48,950 --> 00:02:57,510 And then we'll just copy this and, Print out the Japanese welcome as well. 44 00:02:59,840 --> 00:03:02,438 Save that, now let's try running it. 45 00:03:02,438 --> 00:03:05,842 go run temp.go, 46 00:03:05,842 --> 00:03:09,870 and it prints out our two welcomes, the first in English, second in Japanese. 47 00:03:11,680 --> 00:03:15,910 Now, what if we were to try to use the unexported constant though? 48 00:03:16,950 --> 00:03:19,963 So, let's try adding a third Println call. 49 00:03:21,790 --> 00:03:26,528 And this time, we're going to try to access the welcome packages 50 00:03:26,528 --> 00:03:30,494 klingon, Constant. 51 00:03:30,494 --> 00:03:33,528 But remember, this is an unexported constant, 52 00:03:33,528 --> 00:03:36,790 because it begins with a lowercase letter. 53 00:03:36,790 --> 00:03:41,370 Let's try running this. You see, this time it refuses to compile. 54 00:03:41,370 --> 00:03:45,350 It can't refer to an unexported name, welcome.klingon. 55 00:03:45,350 --> 00:03:49,380 That means that the klingon constant can only be used within the welcome package. 56 00:03:49,380 --> 00:03:52,110 It can't be used from outside it. 57 00:03:52,110 --> 00:03:56,220 We can also download packages from the web and install them on our system for 58 00:03:56,220 --> 00:03:57,900 our go programs to use. 59 00:03:57,900 --> 00:04:02,230 So for example, the golang team here has an example 60 00:04:02,230 --> 00:04:07,970 repository which includes a stringutil package that our programs can use. 61 00:04:07,970 --> 00:04:14,220 So here within the stringutil package I can look in the reverse.go source file and 62 00:04:14,220 --> 00:04:18,010 see that there's a reverse function that I'd like to try using in a package. 63 00:04:18,010 --> 00:04:21,230 So let's try writing a program that uses that. 64 00:04:21,230 --> 00:04:26,584 So I'm gonna import that package, 65 00:04:26,584 --> 00:04:30,509 which has a package path of 66 00:04:30,509 --> 00:04:38,009 github.com/golang/example/stringutil. 67 00:04:38,009 --> 00:04:39,749 stringutil is the package name, and 68 00:04:39,749 --> 00:04:42,060 everything preceding that is the package path. 69 00:04:43,832 --> 00:04:48,850 Once I've imported that package, then I no longer need to use the full package path, 70 00:04:48,850 --> 00:04:56,409 I can just say string, I can just use the package name, stringutil.Reverse. 71 00:04:58,415 --> 00:05:03,742 And I pass it the string that I'd like it to reverse for me. 72 00:05:03,742 --> 00:05:08,760 So the return value of this would be the string that I passed to it backwards. 73 00:05:08,760 --> 00:05:10,292 Let's just print that result out. 74 00:05:10,292 --> 00:05:11,719 fmt.Println. 75 00:05:14,370 --> 00:05:16,580 Hello reversed. 76 00:05:16,580 --> 00:05:21,980 Now if we try to go run temp.go right now, we'll get an error. 77 00:05:21,980 --> 00:05:26,880 Cannot find packet, github.com/golang/example/stringutil. 78 00:05:26,880 --> 00:05:30,750 So first, we'll use the go command's get subcommand to download and 79 00:05:30,750 --> 00:05:32,550 install the package we need. 80 00:05:32,550 --> 00:05:35,608 So I'll just copy and paste the package path and 81 00:05:35,608 --> 00:05:39,518 package name right out of my code down here into the console. 82 00:05:39,518 --> 00:05:47,670 And I'll run the get subcommand of the go command and paste that right here. 83 00:05:49,610 --> 00:05:55,730 The go get command sees that the library package path starts with github.com. 84 00:05:55,730 --> 00:06:00,080 That has no meaning to the go language, but go get knows that GitHub is a site 85 00:06:00,080 --> 00:06:04,130 that hosts git repositories that it can clone the package code from. 86 00:06:04,130 --> 00:06:07,680 So go get downloads the package from GitHub for us. 87 00:06:07,680 --> 00:06:10,400 Now let me refresh our file tree so 88 00:06:10,400 --> 00:06:13,762 that I can show you what changes go get made over here. 89 00:06:13,762 --> 00:06:17,700 go get will create a source subdirectory in your Go workspace directory, 90 00:06:17,700 --> 00:06:19,470 if it doesn't already exist. 91 00:06:19,470 --> 00:06:22,880 And then it'll create subdirectories for each segment of the package path. 92 00:06:22,880 --> 00:06:28,477 So github.com/golang/example/ and 93 00:06:28,477 --> 00:06:32,639 then the stringutil package. 94 00:06:32,639 --> 00:06:37,390 The github.com/go/example repo also contains several other packages. 95 00:06:37,390 --> 00:06:39,070 And go get just clones the whole repo at once. 96 00:06:39,070 --> 00:06:44,800 Now that the stringutil package is installed, we can try rerunning temp.go. 97 00:06:44,800 --> 00:06:45,748 So we'll go back to our console. 98 00:06:45,748 --> 00:06:52,540 And type go run temp.go. 99 00:06:52,540 --> 00:06:54,260 And this time the package will be found. 100 00:06:54,260 --> 00:06:57,850 The program will run successfully, and we'll see our reverse string as output.