如何以新方式构造Golang模块和项目结构 [英] How to Structure Golang Modules and Project structure in the New way
问题描述
似乎从1.11开始使用模块的方式已更改,我正在尝试了解如何从另一个目录引用模块/包.
It seems that the way modules are used since 1.11 has changed , and I am trying to understand how to reference a module / package from another directory.
让我说我有一个文件夹结构 \ root \ module1 \ root \ module2
Lets say I have a folder structure \root\module1 \root\module2
我在每个目录中都有一个go.mod,我可以从\ root目录访问/使用这些模块
I have a go.mod in each directory and I can access / use those modules from the \root directory
如何从module1访问module2.这些模块不会在任何地方发布(也不希望它们被发布)-我只想访问它们.模块2包含我需要在mondule1中使用的类型/结构
How can I access module2 from module1. The modules are not published anywhere ( nor do I want them to be ) - I just want to access them. Module 2 contains types / structs that I need to use in mondule1
亲切问候 马丁
推荐答案
旧方式
必须将Go模块放置在GOPATH中.
OLD WAY
Go modules have to be placed in GOPATH for be used.
当我开始一个新的go项目时,我通常会在gopath中创建一个文件夹
When i start a new go project, i usually create a folder into the gopath
cd $GOPATH
ls
在这里找到3个文件夹
bin pkg src
ls src
>code.cloudfoundry.org github.com github.ibm.com golang.org gopkg.in go.uber.org honnef.co winterdrache.de
在src中,有一些您使用"go get"命令检索的代码.
Into src, there are the code that you retrieve using 'go get' command.
这里的所有内容都可以导入(/导出)到您的软件中.
Everything that is here can be imported(/exported) into your software.
假设此测试项目:
github.ibm.com/
└── Alessio-Savi
└── GoLog-Viewer
├── conf
│ ├── dev.json
│ └── test.json
├── database
│ ├── cloudant
│ │ └── cloudant.go
│ └── db2
│ └── db2.go
├── datastructure
│ └── datastructures.go
├── GinProva.go
├── README.md
├── request
│ └── request.go
└── resources
└── template01.html
注意:数据结构保存在go文件中的正确目录中,以避免导入圈子
NOTE: Data structure are saved in a go file in a properly directory for avoid circle-import
您可以使用以下导入语句导入datastructures.go(或所需的其他文件)
You can import the datastructures.go (or another file that you need) using the following import statement
package mypackage
import(
"github.ibm.com/Alessio-Savi/GoLog-Viewer/datastructure"
)
在其他文件中(与其他项目在同一项目中),您可以简单地使用该软件包并让IDE帮助您(因为该模块/项目位于GOPATH中)
In other file (in the same project as in other) you can simply use the package and let the IDE help you (due to the fact the the module/project is in GOPATH)
要创建新模块,可以使用新的go module init
gotool命令.
In order to create a new module, you can use the new go module init
gotool command.
在公开源代码的情况下,创建新模块的常见方法是:
A common way for create a new module, in case of public source code, is the follwing:
go mod init github.com/username/modulename
这将生成两个文件:
- go.mod
- go.sum
go.mod
文件将包含运行模块所需的每个库/外部golang代码.
go.sum
文件将包含库的哈希.
The go.mod
file will contain every library/external golang code necessary to run your module.
The go.sum
file will contain the hash of the library.
例如,我将使用名为GoGPUtils
的小型通用库.
I'll use for example my little general purpose library, called GoGPUtils
.
mkdir GoGPUtils
cd $_
go mod init github.com/alessiosavi/GoGPUtils
现在,您可以在代码中的go.mod
库中插入所需的库.假设您需要ahocorasick
实现来进行字符串搜索,go.mod
文件将包含以下内容:
Now, you can insert the library that you need in your code in the go.mod
library. Assume that you need the ahocorasick
implementation for work with string search, the go.mod
file will contains the following content:
module github.com/alessiosavi/GoGPUtils
go 1.13
require (
github.com/alessiosavi/ahocorasick v0.0.3
golang.org/x/tools v0.0.0-20191031220737-6d8f1af9ccc0 // indirect
)
在require
部分中,列出了所需的软件包.现在,您可以在代码中导入ahocorasick
库,如下所示:
In the require
section, there are the list of package needed. Now you can import the ahocorasick
library in your code as following:
import (
ahocorasick "github.com/alessiosavi/ahocorasick"
)
这篇关于如何以新方式构造Golang模块和项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!