Go中的相对进口 [英] Relative imports in Go

查看:88
本文介绍了Go中的相对进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下目录结构的go Project

I have a go Project with the following directory structure

utils(pkg)
   | auth.go (has a function names test1)
controllers(pkg)
   | login.go (has a function names test2)

我正在尝试从login.go访问功能test1.这是我所做的

I am trying to access function test1 from login.go. Here is what I have done

import "../utils"

func test2(c *gin.Context) bool{
      utils.test1()
}

但是我总是得到Unresolved reference test1.我是新来的.谁能帮我为什么会收到此错误?

But I always get Unresolved reference test1. I am new to go . Can anyone help why I am getting this error?

推荐答案

在Go中没有相对导入.
您应该考虑使用GOPATH来使用Abs路径:

No there is no relative import in Go.
you should use abs path considering GOPATH:

GOPATH环境变量指定工作空间的位置.在开发Go代码时,很可能需要设置唯一的环境变量.首先,创建一个工作区目录并相应地设置GOPATH.请参阅: https://golang.org/doc/code.html#GOPATH

The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code. To get started, create a workspace directory and set GOPATH accordingly. see: https://golang.org/doc/code.html#GOPATH

导入路径

导入路径是唯一标识软件包的字符串.包的导入路径与其在工作空间中的位置相对应 或在远程存储库中(如下所述).

Import paths

An import path is a string that uniquely identifies a package. A package's import path corresponds to its location inside a workspace or in a remote repository (explained below).

标准库中的软件包具有简短的导入路径 例如"fmt"和"net/http".对于您自己的包裹,您必须选择一个 基本路径,该路径不太可能与将来添加到 标准库或其他外部库.

The packages from the standard library are given short import paths such as "fmt" and "net/http". For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries.

如果将代码保存在源存储库中的某个位置,则应使用该源存储库的根作为基本路径.为了 例如,如果您在github.com/user上有一个GitHub帐户,则应该 作为您的基本路径.

If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.

请注意,您无需将代码发布到远程存储库 在构建它之前.这是组织代码的好习惯 好像您将有一天将其发布.在实践中,您可以选择任何 任意路径名,只要它对标准库是唯一的 和更大的Go生态系统.

Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code as if you will publish it someday. In practice you can choose any arbitrary path name, as long as it is unique to the standard library and greater Go ecosystem.

示例:

此示例假设您已在操作系统环境中设置了GOPATH=/goworkdir.

This example assumes you have set GOPATH=/goworkdir in your OS environment.

文件:goworkdir/src/project1/utils/auth.go

package utils

func Test1() string {
    return "Test1"
}

文件:goworkdir/src/project1/controllers/login.go

package controllers

import "project1/utils"

func Test2() string {
    return utils.Test1()
}

文件:goworkdir/src/project1/main.go

package main

import (
    "fmt"
    "project1/controllers"
)

func main() {
    fmt.Println(controllers.Test2())
}

现在,如果您按go run main.go键,应该会看到输出:

Now if you go run main.go you should see output:

Test1

这篇关于Go中的相对进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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