使用Go模块组织程序包中的本地代码 [英] Organize local code in packages using Go modules

查看:82
本文介绍了使用Go模块组织程序包中的本地代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$GOPATH之外使用Go模块(go版本> = 1.11)时,我找不到将main.go中的某些代码分解为本地程序包的方法.

I can not find a way to factor out some code from main.go into a local package when using Go modules (go version >= 1.11) outside of $GOPATH.

我没有导入任何需要包含在go.mod中的外部依赖项,我只是想在本地组织此Go模块的源代码.

I am not importing any external dependencies that need to be included into go.mod, I am just trying to organize locally the source code of this Go module.

文件main.go:

package main

// this import does not work
import "./stuff"

func main() {
    stuff.PrintBaz()
}

文件./stuff/bar.go(伪装为本地软件包):

The file ./stuff/bar.go (pretending to be a local package):

package stuff

import "log"

type Bar struct {
    Baz int
}

func PrintBaz() {
    baz := Bar{42}
    log.Printf("Bar struct: %v", baz)
}

文件go.mod(命令go mod init foo):

module foo

go 1.12

执行go run main.go时:

  • 如果我import "./stuff",那么我会看到build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.
  • 如果我import "stuff",那么我会看到build command-line-arguments: cannot load stuff: cannot find module providing package stuff.
  • 如果我import stuff "./stuff"具有软件包别名,那么我会再次看到:build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.
  • If I import "./stuff", then I see build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.
  • If I import "stuff", then I see build command-line-arguments: cannot load stuff: cannot find module providing package stuff.
  • If I import stuff "./stuff" with a package alias, then I see again: build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff.

我找不到使本地软件包与go模块一起使用的方法.

I can not find a way to make local packages work with go modules.

  • 上面的代码怎么了?
  • 如何在使用Go模块(文件go.mod)定义的项目中将本地包导入其他Go代码?
  • What's wrong with the code above?
  • How can I import local packages into other Go code inside a project defined with Go modules (file go.mod)?

推荐答案

首先,您必须为项目选择一个名称并将其写入go.mod文件.该名称属于项目的根目录.您创建的每个新软件包都必须位于其自己的子目录中,并且其名称应与目录名称匹配.

First you have to choose a name for your project and write it to go.mod file. This name belongs to root directory of the project. Each new package you create must be located inside it's own subdirectory and its name should match directory name.

go.mod:

module myprojectname

或(首选方法,有关详细信息,请参见@ typical182的下面的答案)

or (preferred way, see @typical182's answer below for details)

module github.com/myname/myproject

然后导入项目的程序包,例如:

Then import your project's packages like:

import myprojectname/stuff

import github.com/myname/myproject/stuff

软件包stuff的文件应该位于项目的stuff目录中.您可以根据需要命名这些文件.

Files of package stuff should be located inside project's stuff directory. You name these files as you like.

还可以创建更深的项目结构.例如,您决定将源代码文件与其他文件分开(例如应用程序配置,docker文件,静态文件等).让我们将stuff目录移到pkg内,在pkg/stuff内的每个go文件仍然具有stuff包名称.要导入东西包,只需编写:

Also it's possible to create deeper project structure. For instance, you decided to separate source code files from other ones (like app configs, docker files, static files, etc...). Let's move stuff directory inside pkg, every go file inside pkg/stuff still have stuff package name. To import stuff package just write:

import myprojectname/pkg/stuff

没有什么能阻止您在层次结构中创建更多级别,如github.com/myuser/myproject/pkg/db/provider/postgresql,其中:

Nothing stops you from creating more levels in the hierarchy like github.com/myuser/myproject/pkg/db/provider/postgresql, where:

  • github.com/myuser/myproject-项目名称.
  • postgresql-程序包名称.
  • pkg/db/provider/postgresql-相对于项目根目录的软件包路径.
  • github.com/myuser/myproject - project name.
  • postgresql - package name.
  • pkg/db/provider/postgresql - path to the package relative to project's root.

您可以在此处阅读有关go模块的更多信息: https://github.com/golang/go/wiki/模块

You can read more about go modules here: https://github.com/golang/go/wiki/Modules

检出此存储库以获取有关项目组织中使用的各种模式的有用信息: https://github.com/golang-standards/project-layout 如果进入pkg目录,则会发现哪些开源项目在其结构中使用pkg目录.

Check out this repository to get useful information about various patterns used in project organizing: https://github.com/golang-standards/project-layout If you go inside pkg directory you will find out which open source projects use pkg directory in their structure.

这篇关于使用Go模块组织程序包中的本地代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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