Packages----Go组织项目结构-包

279 阅读1分钟

原文(英文阅读理解)

Every Go program is made up of packages.
Programs start running in package main.
This program is using the packages with import paths "fmt" and "math/rand".
By convention, the package name is the same as the last element of the import path. For instance, the "math/rand" package comprises files that begin with the statement package rand.
Note: The environment in which these programs are executed is deterministic, so each time you run the example program rand.Intn will return the same number.
(To see a different number, seed the number generator; see rand.Seed. Time is constant in the playground, so you will need to use something else as the seed.)

代码

package main
import (
	"fmt"
	"math/rand"
)
func main() {
	fmt.Println("My favorite number is", rand.Intn(10))
}

翻译

每个Go程序都是由package组成的.(这个地方的程序应该理解成最后打包成的一个可运行的东西,类似于jar,或者exe,sh文件等)
Go程序是以运行package main开始的,也就是说,Go开始运行的时候是寻找一个叫main的package,然后运行里面的main函数.
这个Go程序使用了fmt和math/rand包.
按照惯例,包名称和倒入路径的最后一个文件夹名字相同,例如:math/rand这个包名就叫rand

Note(注意):这个程序返回的Intn(10)是确定的一个数字,任何时候运行返回的数字都一样
(如果你想获取一个随机的数字,去看下数字种子(seed))

总结

这一个小节,其实就是扯了下,怎么导入pacakge,使用别人写好的东西,其实GoLand(一个开发Go的IDE工具)有提示,不用担心这个.

实验项目

我自己的小白鼠Go项目mouse项目,自己可以clone一份:github.com/liuxuzxx/mo…