课程: Hands-On Introduction: Go

Code organization with packages - Go教程

课程: Hands-On Introduction: Go

Code organization with packages

- [Instructor] Let's open up our exploration of Go by looking at how Go organizes code and how things like functions are accessed across those organizational boundaries. Go Code is organized through the use of packages that are specified at the beginning of each file that make up your programs and libraries. Let's open up an example. I'll not close the sidebar. I'll be using a quick open feature of the code spaces editor to quickly open files in the future. The file locations will be at the top of each example we look at, so you can follow along. Better yet, launch your own code space and don't hesitate to pause the video at any point so you can code along. Let's continue with our lesson. A package declaration is required for every Go file in your project. The package main and function main specify the entry point for your programs as seen in this example. Shipping our programs involves compiling them to get an executable. I'll open up my terminal, and issue a Go bill command with a flag that specifies the name I want the executable to have along with the folder where this main.go file exists. Listing out the concepts of the directory we see that executable was created. We can even execute the program. We'll often combine those two steps into a single one by issuing the go run command instead. Let's create terminal, to ensure go run packages. Behind the scenes, this command will do the compilation, run the program and discard of the temporary executable for us automatically. Though compilable and runable this program doesn't do much yet. Let's see how we can make it a little more interesting as we discuss imports next.

内容