前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang-101-hacks(3)——包

golang-101-hacks(3)——包

作者头像
羊羽shine
发布2019-05-29 16:34:58
3670
发布2019-05-29 16:34:58
举报
文章被收录于专栏:Golang开发Golang开发

注:本文是对golang-101-hacks中文翻译,原文地址 在“Go”中,包分为两种类型: (1) main包:用于生成可执行的二进制文件,main函数是程序的入口点。下面以helllo.go 为例:

package main

import "greet"

func main() {
    greet.Greet()
}

(2)其他类型的包也可以在细分成两类: 库文件包:用来生成可以被其他人重用的目标文件。如greet.go这个文件

package greet

import "fmt"

func Greet() {
    fmt.Println("Hello 中国!")
}

b)另外的包主要是特殊用途的,比如测试。 当程序需要引用“Go”标准包(“

GOROOT”)或第三方包(“
GOROOT”)或第三方包(“

GOPATH”)时,在顶部声明“import”:

import "fmt"
import "github.com/NanXiao/stack" 

Or:

import (
    "fmt"
    "github.com/NanXiao/stack"
)

在上面的例子中,为了导入相关的类包 需要声明'fm和github.com/NanXiao/stack的包路径 In the above examples, the "fmt" and "github.com/NanXiao/stack" are called import path, which is used to find the relevant package. 你也可以看到如下的用法 You may also see the following cases:

import m "lib/math" // 使用m作为math包的别名
import . "lib/math" // 当使用点号时math包时可以省略包名

如果执行go install命令找不到指定的包,它会报告如下错误消息 If the go install command can't find the specified package, it will complain the error messages like this:

... : cannot find package "xxxx" in any of:
        /usr/local/go/src/xxxx (from $GOROOT)
        /root/gowork/src/xxxx (from $GOPATH)

为了避免包引用出现冲突,需要确保包的路径地址的唯一性,例如以github仓库地址作为标识 To avoid library conflicts, you'd better make your own packages' path the only one in the world: E.g., your githubrepository destination:

 github.com/NanXiao/...

尽管不是强制要求,通常良好的编程习惯是包名称使用包路径地址最后的结尾项名称。 Conventionally, your package name should be same with the last item in import path; it is a good coding habit though not a must.

Reference: The Go Programming Language.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档