Join David Gassner for an in-depth discussion in this video Creating packages as function libraries, part of Learning Go.
- You can group functions together into their own separate packages declared in separate source code files. You can then use them as libraries of functions that are reusable among multiple applications. For this demonstration I'll work in a complete Go workspace that I set up earlier in the course. In the Exercise Files I have a file called libraries.go. It picks up where my last exercise left off. I've declared a couple of custom functions named FullName and FullNameNakedReturn.
And my goal is to take those functions and put them into their own separate library. First, I'll go to File Explorer on Windows, or if you're working on Mac, go to Finder. And make a copy of this libraries.go file. I'll just copy it to my clipboard. Then I'll navigate to my Go workspace folder, that has subfolders of bin, package, and source. And then I'll double-click into the source folder, and I'll create a new folder that I'll also name libraries.
That will be my main package for this new application, and I'll paste the file into place. Now, I'll go back to Visual Studio Code and I'll open up my workspace source code folder. I'll select File, Open Folder, and I'll go to c:\gowork\src. So here's my file that I've copied over to the new subfolder under my workspace. Now the next step will be to create a new subdirectory underneath the workspace source code folder for my library.
I'm going to a command prompt, and I'll go up one level in the directory structure. And then I'll make a new folder that I'll name stringutil. So now I have three directories. My original palindrome application and then directories for this exercise, libraries, which will have my main file, and stringutil. I'll go back to Visual Studio Code, and there's the new subfolder that I just created. Now I'll create a New File in that folder, and I'll name it stringutil.go.
I'll come back to my original file and I'm going to copy some code into place. First I'll select the package statement. I'll copy that, and then I'll paste it into the new file. And I'll change the package declaration to match the name of the file and the name of the folder, with package stringutil. Now I'll go back to libraries.go, and I'll grab these two functions and I'll copy those. And then I'll go back to the stringutil file and I'll paste them into place.
Notice that I don't have any import statement in this file, and that's because I'm not calling any other packages from here. So I'll save that change, and that file is done. Now I'll go back to libraries.go, and I'll delete these functions from this file. Next I need to import my libraries file. I'll change my import statement, I'll put in a pair of parentheses, and move the fmt import up here.
And then I'll add an import for stringutil. Then I'll come down here to where I'm calling the functions. FullName and FullNameNakedReturn, and I'll add the name of that package as a prefix, and separate it from the function name with a dot. And I'll do that on both function calls. And so now all the code works is done. I've moved my functions over to the stringutil file, and I've given it a package name of stringutil up here.
And in my main file which has the package and function named main, I've imported the library and now I'm calling the functions from the library using dot syntax. Now I'll go back to my command prompt, and I'll make sure that I've set my GOPATH environment variable. You might have set this persistently in an earlier exercise, but if you haven't, you should set it now. Set it to match the name of the base directory that defines your workspace. That's the parent directory of the source code folder.
Then switch to your libraries folder, and type go install. The compiler looks at that import statement. It sees stringutil, and then it goes back to the source code folder, and it finds the appropriate directory and file. And then finally it creates your executable, which is called in my example, libraries.exe. And I'll run the application, and there's the result. So this is a simple example of segmenting your functions into their own source code files.
It makes it easy to structure your application so everything isn't in a single source code file. And it also makes it easy to share source code among multiple applications.
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server