golang模块和本地包 [英] golang modules and local packages

查看:426
本文介绍了golang模块和本地包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何使用go1.11模块来组织我的golang项目.我尝试了几种选择,但没有一个起作用.

I'm trying to understand how to organize my golang project using go1.11 modules. I tried several options, but none of them worked.

我在应用程序文件夹下的主程序包中有一些代码,并且在主程序包中使用了本地程序包.

I have some code in the main package under the application folder and a local package that the main package uses.

$GOPATH
+ src
  + application/
    + main/
      + main.go
      + otherFileUnderMainPackage.go
    + aLocalPackage/
      + someCode.go
      + someCode_test.go
      + someMoreCode.go
      + someMoreCode_test.go

在主包中的文件导入../aLocalPackage.当我按go build main/*.go进行编译时,即可正常工作.

Files in the main package, imports ../aLocalPackage. When I compile by go build main/*.go it's working.

然后,我运行go mod init application: V.0.9.9并获得了go.mod文件,但是构建始终失败.我总是会收到有关找不到本地软件包的错误:build application:V0.9.9/main: cannot find module for path _/.../src/application/aLocalPackage.我还尝试将本地程序包放在src/下,将其放在main/下,等等,但是这些方法都不适合我.

Then, I ran go mod init application: V.0.9.9 and got the go.mod file, but the build always fails. I always get error about not finding the local package: build application:V0.9.9/main: cannot find module for path _/.../src/application/aLocalPackage. I also tried to place the local package right under src/, place it under main/ etc. but none of these methods worked for me.

使用模块和本地软件包的方式是什么?

What is the way to use modules and local packages?

谢谢.

推荐答案

我写了一篇博客文章,介绍如何使用模块启动您的第一个Go项目.

I have written a blogpost on how to start your first Go project using modules.

https://marcofranssen.nl/start-on-your- first-golang-project/

通常,它可以简化为仅在系统上的某个位置创建一个新文件夹(不必位于GOPATH中).

In general it boils down to just create a new folder somewhere on your system (doesn't have to be in GOPATH).

mkdir my-project
cd my-project
go mod init github.com/you-user/my-project

这将创建go.mod文件.现在,您可以简单地创建项目布局并开始构建所需的任何内容.

This will create the go.mod file. Now you can simply create your project layout and start building whatever you like.

也许我的其他博客之一可以使您对做事有更多的启发.

Maybe one of my other blogs can inspire you a bit more on how to do things.

https://marcofranssen.nl/categories/software-development/golang/

这篇关于golang模块和本地包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆