Go 中的相对导入 [英] Relative imports in Go

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

问题描述

我有一个目录结构如下的 go 项目

utils(pkg)|auth.go(有一个函数名 test1)控制器(包)|login.go(有一个函数名 test2)

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

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

但我总是得到Unresolved reference test1.我是新来的.任何人都可以帮助我为什么会收到此错误?

解决方案

Go 中没有相对导入.
考虑到 GOPATH,您应该使用绝对路径:

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

导入路径

导入路径是唯一标识包的字符串.包的导入路径对应于它在工作区中的位置或在远程存储库中(如下所述).

标准库中的包被赋予了短的导入路径例如fmt"和net/http".对于您自己的包裹,您必须选择一个不太可能与未来添加到的基本路径发生冲突标准库或其他外部库.

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

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

示例:

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

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

包实用程序func Test1() 字符串 {返回Test1"}

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

包控制器导入project1/utils"func Test2() 字符串 {返回 utils.Test1()}

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

包主进口 (fm"项目 1/控制器")功能主(){fmt.Println(controllers.Test2())}

现在如果你运行 main.go 你应该看到输出:

Test1

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)

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()
}

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

解决方案

No there is no relative import in Go.
you should use the absolute path considering 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).

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.

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.

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.

Example:

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

File: goworkdir/src/project1/utils/auth.go

package utils

func Test1() string {
    return "Test1"
}

File: goworkdir/src/project1/controllers/login.go

package controllers

import "project1/utils"

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

File: goworkdir/src/project1/main.go

package main

import (
    "fmt"
    "project1/controllers"
)

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

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

Test1

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

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