官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 白银市| 益阳市| 山阳县| 万全县| 镇雄县| 乌拉特后旗| 黔江区| 江油市| 青川县| 那坡县| 武冈市| 西丰县| 肥乡县| 林周县| 昌邑市| 通辽市| 马边| 肃南| 静安区| 富锦市| 太保市| 永城市| 德令哈市| 禹州市| 潞西市| 衡东县| 芒康县| 永年县| 兴业县| 长春市| 台江县| 东城区| 南雄市| 鄢陵县| 喜德县| 辽阳县| 诏安县| 遵义市| 隆林| 株洲市| 罗平县|