且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用自定义包

更新时间:2023-02-25 21:07:46

首先,请务必阅读并理解如何编写Go代码" 文档.

First, be sure to read and understand the "How to write Go code" document.

实际答案取决于您的自定义程序包"的性质.

The actual answer depends on the nature of your "custom package".

如果打算用于一般用途,请考虑使用所谓的"Github代码布局" .基本上,您将库设为一个单独的go get -table项目.

If it's intended to be of general use, consider employing the so-called "Github code layout". Basically, you make your library a separate go get-table project.

如果您的库供内部使用,则可以这样:

If your library is for internal use, you could go like this:

  1. 将包含库文件的目录放置在项目目录下.
  2. 在项目的其余部分,请使用相对于包含该项目的工作空间的根目录的路径来引用该库.

演示:

src/
  myproject/
    mylib/
      mylib.go
      ...
    main.go

现在,在***main.go中,您可以import "myproject/mylib",它将正常运行.

Now, in the top-level main.go, you could import "myproject/mylib" and it would work OK.