将go项目分解为子文件夹 [英] Break up go project into subfolders

查看:179
本文介绍了将go项目分解为子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将项目分解为多个子文件夹.

I want to break my project up to subfolders.

我想要这样的代码结构:

I want this code structure:

├── main.go
└── models
    └── user.go

main.go在哪里:

Where main.go is:

package main

import (
  "fmt"
  "./models"
)

func main(){
  fmt.Println(User{"new_user"})

}

user.go是:

package models

type User struct {
  Login string
}

但是未在主软件包中定义用户,并且导入引发警告已导入且未使用".

But User is not defined in main package and import raise warning "imported and not used".

我做错了什么?我的项目很简单(不是这样的示例,而只有很少的文件(控制器和模型)),并且我想要一个简单的结构.

What am I doing wrong? My project is simple (not such a example but just with few files (controllers and models)) and I want a simple structure.

也许我完全以错误的方式来做?

Maybe I doing it in completely wrong way?

问题项目在这里: https://github.com/abonec/go_import_problem

推荐答案

我最近通过使用 go模块实现了这一目标.

I recently achieved this by using go modules.

Golang从 go v1.11.1 开始引入了对模块的初步选择支持.旨在完全消除荒谬的$GOPATH必要性.现在,您不仅可以在任何普通目录(例如~/development)中拥有版本化的依赖项,而且基本上可以具有类似于名称空间和子目录的外观.您可以启用此功能通过使用以下环境变量调用go命令:GO111MODULE=on.

Golang introduced preliminary opt-in support for modules as of go v1.11.1 which is intended to completely remove the, frankly, absurd $GOPATH necessity. Not only can you now have versioned dependencies in any normal directory such as ~/development, but you can basically have something that looks like namespaces and sub-directories. You can enable this feature by invoking the go command with the following environment variable: GO111MODULE=on.

Go v1.11.3预计默认启用模块,并将于2019年8月推出.

Go v1.11.3 expects to enable modules by default and is slated for August 2019.

这是示例目录结构(您可能通常会用其他一些语言找到它).

Here is an example directory structure (that you might find typically in some other languages).

~/Dev/my-app
 ├── src/
 │   ├── one/
 │   │   ├── two/
 │   │   │   └── two.go
 │   │   └── one.go
 │   └── zero.go
 ├── go.mod
 └── app.go

该应用程序称为my-app,它将是app.go的模块名称.我们在go.mod中定义了一次,然后子目录中的所有其他go文件都将自动导入,就像它们是已命名空间一样.

The application is called my-app, which will be the module name for app.go. We define this once in go.mod and then each of all the other go files in subdirectories will automatically be importable as if they were namespaced.

鉴于以上情况,假设two.go包含名为Two的函数,则可以使用my-app/src/one/two将其导入app.go.

Given the above, two.go, assuming it contains a function named Two, will be importable in app.go by using my-app/src/one/two.

这是您需要实现的目标:

Here's what you need to do to achieve this:

module my-app

two.go

package two

func Two() string {
    return "I'm totally not supposed to be using go modules for this"
}

app.go

package main

import "my-app/src/one/two"

func main() {
    two.Two()
}

如果要将另一个文件放在2/中,则只要在新文件中提供TheNewFunc()即可使用two.TheNewFunc().

If you were to place another file within two/, then you would simply use two.TheNewFunc() as long as you made TheNewFunc() available within the new file.

我创建了一个非常简单的 GitHub存储库,您可以将其作为示范.

I created a very simple GitHub repo which you can check out as a demonstration.

这篇关于将go项目分解为子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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