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

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.

主站蜘蛛池模板: 永年县| 威远县| 桦南县| 大化| 阳信县| 台中市| 巴林右旗| 许昌县| 若尔盖县| 临泽县| 手游| 嘉善县| 灵山县| 红原县| 民勤县| 西昌市| 云龙县| 中方县| 景谷| 津南区| 千阳县| 凭祥市| 邳州市| 大埔区| 汝州市| 全南县| 来安县| 香港 | 肥乡县| 芜湖市| 新乡县| 安康市| 彰化县| 广安市| 西峡县| 新竹县| 化德县| 新昌县| 盐池县| 古交市| 惠东县|