Golang - 包名与导入路径

路漫漫其修远兮,吾将上下而求索。

什么是路径

1
2
3
4
5
6
7
1
2
3
4
5
6
7
import (
	"fmt"
    "math/rand"
)
func main() {
    fmt.Println(rand.Intn(100))
}

这段代码中 math/rand 就代表路径。

什么是包名

1
2
3
4
5
6
7
1
2
3
4
5
6
7
import (
	"fmt"
    "math/rand"
)
func main() {
    fmt.Println(rand.Intn(100))
}

这段代码中 rand 就代表包名,在代码里使用包名来调用包里的函数。

包名与路径的关系

导入路径 包名
“fmt” fmt
“math” math
“archive/zip” zip
“math/rand” rand

通过上面的表格可以发现,如果导入路径为”math”,则包名为 math,如果导入路径为”math/rand”,则包名为 rand。