java子包 父包可见性_了解Go中的软件包可见性

java子包 父包可见性

介绍 (Introduction)

When creating a package in Go, the end goal is usually to make the package accessible for other developers to use, either in higher order packages or whole programs. By importing the package, your piece of code can serve as the building block for other, more complex tools. However, only certain packages are available for importing. This is determined by the visibility of the package.

在Go中创建程序包时 ,最终目标通常是使程序包可以供其他开发人员使用,无论是高级程序包还是整个程序。 通过导入软件包 ,您的代码段可以用作其他更复杂工具的构建块。 但是,仅某些软件包可用于导入。 这由包装的可见性决定。

Visibility in this context means the file space from which a package or other construct can be referenced. For example, if we define a variable in a function, the visibility (scope) of that variable is only within the function in which it was defined. Similarly, if you define a variable in a package, you can make it visible to just that package, or allow it to be visible outside the package as well.

在这种情况下, 可见性意味着可以从中引用包或其他构造的文件空间。 例如,如果我们在函数中定义变量,则该变量的可见性(作用域)仅在定义该变量的函数内。 同样,如果在程序包中定义变量,则可以使该变量仅对该程序包可见,或者也允许它在程序包外部可见。

Carefully controlling package visibility is important when writing ergonomic code, especially when accounting for future changes that you may want to make to your package. If you need to fix a bug, improve performance, or change functionality, you’ll want to make the change in a way that won’t break the code of anyone using your package. One way to minimize breaking changes is to allow access only to the parts of your package that are needed for it to be used properly. By limiting access, you can make changes internally to your package with less of a chance of affecting how other developers are using your package.

在编写符合人体工程学的代码时,尤其是考虑到将来可能要对软件包进行的更改时,仔细控制软件包的可见性非常重要。 如果您需要修复错误,提高性能或更改功能,则需要以不会破坏任何使用您软件包的代码的方式进行更改。 最小化重大更改的一种方法是仅允许访问包装中正确使用所需的部分。 通过限制访问,您可以在内部对程序包进行更改,而不会影响其他开发人员使用程序包的机会。

In this article, you will learn how to control package visibility, as well as how to protect parts of your code that should only be used inside your package. To do this, we will create a basic logger to log and debug messages, using packages with varying degrees of item visibility.

在本文中,您将学习如何控制包的可见性,以及如何保护只应在包内使用的部分代码。 为此,我们将创建一个基本的记录器,以使用具有不同程度的项目可见性的包来记录和调试消息。

先决条件 (Prerequisites)

To follow the examples in this article, you will need:

要遵循本文中的示例,您将需要:

  • A Go workspace set up by following How To Install Go and Set Up a Local Programming Environment. This tutorial will use the following file structure:

    通过遵循如何安装Go和设置本地编程环境来设置 Go工作区。 本教程将使用以下文件结构:

出口和未出口项目 (Exported and Unexported Items)

Unlike other program languages like Java and Python that use access modifiers such as , , or to specify scope, Go determines if an item is and through how it is declared. Exporting an item in this case makes it outside the current package. If it’s not exported, it is only visible and usable from within the package it was defined.

与其他使用访问修饰符(例如 , 或来指定范围的Java和Python等其他程序语言不同,Go通过声明项的方式确定是还是不项。 在这种情况下,导出项目将使其在当前包外部 。 如果未导出,则仅在定义的包中可见并可用。

This external visibility is controlled by capitalizing the first letter of the item declared. All declarations, such as , , , , etc., that start with a capital letter are visible outside the current package.

外部可见性通过大写声明的项目的首字母来控制。 所有以大写字母开头的声明,例如 , , , 等,都可以在当前包外部看到。

Let’s look at the following code, paying careful attention to capitalization:

让我们看下面的代码,注意大写:

greet.go greet.go

This code declares that it is in the package. It then declares two symbols, a variable called , and a function called . Because they both start with a capital letter, they are both and available to any outside program. As stated earlier, crafting a package that limits access will allow for better API design and make it easier to update your package internally without breaking anyone’s code that is depending on your package.

此代码声明它在包中。 然后,它声明两个符号,一个名为的变量和一个名为的函数。 因为它们都以大写字母开头,所以它们都可以并可以用于任何外部程序。 如前所述,设计一个限制访问权限的软件包将有助于更好的API设计,并使内部更新软件包变得更加容易,而不会破坏依赖于软件包的任何人的代码。

定义包装的可见性 (Defining Package Visibility)

To give a closer look at how package visibility works in a program, let’s create a package, keeping in mind what we want to make visible outside our package and what we won’t make visible. This logging package will be responsible for logging any of our program messages to the console. It will also look at what level we are logging at. A level describes the type of log, and is going to be one of three statuses: , , or .

为了更仔细地查看程序包中可见性的工作方式,让我们创建一个程序包,同时牢记我们要在程序包之外显示的内容以及不希望显示的内容。 此日志记录包将负责将我们的任何程序消息记录到控制台。 它还将查看我们正在记录的级别 。 一个级别描述了日志的类型,它将成为以下三种状态之一: , 或 。

First, within your directory, let’s create a directory called to put our logging files in:

首先,在您的目录中,创建一个名为的目录,以将我们的日志文件放入:

  • mkdir logging

    mkdir日志记录

Move into that directory next:

接下来进入该目录:

  • cd logging

    cd记录

Then, using an editor like nano, create a file called :

然后,使用诸如nano之类的编辑器,创建一个名为的文件:

  • nano logging.go

    纳米测井

Place the following code in the file we just created:

将以下代码放在我们刚刚创建的文件中:

logging/logging.go logging / logging.go

The first line of this code declared a package called . In this package, there are two functions: and . These functions can be called by any other package that imports the package. There is also a private variable called . This variable is only accessible from within the package. It is important to note that while the function and the variable both have the same spelling, the function is capitalized and the variable is not. This makes them distinct declarations with different scopes.

此代码的第一行声明了一个称为的程序包。 在此软件包中,有两个函数: 和 。 导入软件包的任何其他软件包均可调用这些函数。 还有一个称为的私有变量。 此变量只能从包中访问。 重要的是要注意,尽管函数和变量的拼写是相同的,但函数是大写的而变量不是。 这使它们具有不同范围的不同声明。

Save and quit the file.

保存并退出文件。

To use this package in other areas of our code, we can it into a new package. We’ll create this new package, but we’ll need a new directory to store those source files in first.

要在我们代码的其他区域使用此包,我们可以其到新包中 。 我们将创建此新程序包,但首先需要一个新目录来存储这些源文件。

Let’s move out of the directory, create a new directory called , and move into that new directory:

让我们移出目录,创建一个名为的新目录,然后移至该新目录:

  • cd ..

    光盘..

  • mkdir cmd

    mkdir cmd

  • cd cmd

    cd cmd

Create a file called in the directory we just created:

在我们刚创建的目录中创建一个名为的文件:

  • nano main.go

    纳米main.go

Now we can add the following code:

现在我们可以添加以下代码:

cmd/main.go cmd / main.go

We now have our entire program written. However, before we can run this program, we’ll need to also create a couple of configuration files for our code to work properly. Go uses Go Modules to configure package dependencies for importing resources. Go modules are configuration files placed in your package directory that tell the compiler where to import packages from. While learning about modules is beyond the scope of this article, we can write just a couple lines of configuration to make this example work locally.

现在,我们编写了整个程序。 但是,在运行该程序之前,我们还需要创建几个配置文件,以使我们的代码正常运行。 Go使用Go Modules配置用于导入资源的程序包依赖项。 Go模块是放置在软件包目录中的配置文件,告诉编译器从何处导入软件包。 尽管关于模块的学习超出了本文的范围,但我们仅需编写几行配置即可使该示例在本地运行。

Open the following file in the directory:

在目录中打开以下文件:

  • nano go.mod

    纳米go.mod

Then place the following contents in the file:

然后将以下内容放入文件中:

go.mod go.mod

The first line of this file tells the compiler that the package has a file path of . The second line tells the compiler that the package can be found locally on disk in the directory.

该文件的第一行告诉编译器, 软件包的文件路径为 。 第二行告诉编译器,可以在目录中的磁盘上本地找到软件包 。

We’ll also need a file for our package. Let’s move back into the directory and create a file:

我们还将需要一个文件作为包。 让我们回到目录并创建一个文件:

  • cd ../logging

    cd ../记录

  • nano go.mod

    纳米go.mod

Add the following contents to the file:

将以下内容添加到文件中:

go.mod go.mod

This tells the compiler that the package we created is actually the package. This makes it possible to import the package in our package with the following line that we wrote earlier:

这告诉编译器我们创建的包实际上是包。 这样就可以使用前面编写的以下行将包导入到我们的包中:

cmd/main.go cmd / main.go

You should now have the following directory structure and file layout:

您现在应该具有以下目录结构和文件布局:

Now that we have all the configuration completed, we can run the program from the package with the following commands:

现在,我们已经完成了所有配置,我们可以使用以下命令从包运行 :

  • cd ../cmd

    cd ../cmd

  • go run main.go

    去运行main.go

You will get output similar to the following:

您将获得类似于以下内容的输出:

The program will print out the current time in RFC 3339 format followed by whatever statement we sent to the logger. RFC 3339 is a time format that was designed to represent time on the internet

来源:cukw6666

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年7月10日
下一篇 2020年7月10日

相关推荐