- Hands-On Full Stack Development with Go
- Mina Andrawos
- 525字
- 2021-07-02 12:33:29
Packages
Any Go program consists of one or more packages. Each package is basically a folder, which contains one or more Go files. Every single Go code file you write must belong to a package. Package folders are found inside the src folder of your Go workspace.
When you write Go code, you declare your package name at the very top of your Go file. Here is what this looks like in code:
package mypackage
Here, mypackage is the name of the package that my Go file belongs to. It's idiomatic in Go to have your package name in lower case letters. It is usually preferable to name your package folder the same as your package name. So, when you create a new package, simply create a new folder with your package name, and then create your package files inside that folder.
To import an external package and use it in your own package, you need to use the import keyword. For example, a popular package in Go's standard library is the fmt package, which allows you to write data to the standard output (that is, write to your console screen). Let's assume we want to use the fmt package from within our package. Here is what the code would look like:
package mypackage
import "fmt"
Some package folders can exist inside folders of other packages. For example, the folder that contains the rand package in Go, which is used to generate random numbers, exists inside the folder that contains the math package in Go. To import a package like that, you need to use the following syntax:
import "math/rand"
Now, what if we would like to import multiple packages at once? It's easy—the syntax will end up looking like this:
import (
"fmt"
"math/rand"
)
Go does not allow you to import a package and then not use it to ensure that your code is clean and concise. However, there are some cases (which we'll cover later in this book) where you will want to load a package, but not use it directly. This can be accomplished by appending an underscore before the package name in the import statement. Here is what this would look like:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
The most famous package name is main. Your main package is the first package that runs in your Go program.
To compile a Go program, you will need to navigate to the folder where your main package lives in the console, and then type the following command:
go install
This command will compile your Go program and then place the resulting binary in the bin folder of your workspace.
Alternatively, you can run the following command:
go build
This command will compile and then deploy the resulting binary in the current folder.
If you would like to specify an output path and a filename, you can run the following command:
go build -o ./output/myexecutable.exe
This will compile your code and then package it in an executable called myexecutable.exe at the specified output folder. If your operating system is not Windows, you can ignore the exe extension in the preceding example.
- Mastering AWS Lambda
- Mastering Linux Network Administration
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- The Professional ScrumMaster’s Handbook
- 移動互聯網軟件開發實驗指導
- Unity&VR游戲美術設計實戰
- Flask Web開發:基于Python的Web應用開發實戰(第2版)
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Beginning C# 7 Hands-On:The Core Language
- MongoDB Cookbook
- Ubuntu Server Cookbook
- Raspberry Pi開發實戰
- Visual C++程序開發范例寶典
- Learning Swift
- 前端Serverless:面向全棧的無服務器架構實戰