Golang找不到本地路径的模块 [英] golang cannot find module for local path

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

问题描述

我想从主程序包中推出一些API到单独的程序包中:

I want to push out some of API, from main package into separate package:

myapp/
    main.go
    myapi/
        myapi.go

在我有的main.go内部

package main

import "./myapi"

...

myapi.go开头为:

package myapi

...

当我尝试运行main时,似乎找不到我的myapi #include.它给了我以下错误:

When I am trying to run main, it seems like it can't find my myapi #include. It gives me following error:

D:\go\myapp> go run .
build _/D_/go/myapp/myapi: cannot find module for path _/D_/go/myapp/myapi

我来自C/C ++领域,如何在golang的子文件夹中包含它是非常不明显的.你能帮我吗?

I came from C/C++ world, and it's extremely unobvious, how to include from subfolder in golang. Could you help me with this?

推荐答案

Go使用了称为模块路径"的模块.这些是标识您的软件包的路径.它们不一定与文件系统有关.

Go uses something called Module Paths. These are paths that identify your packages. They are not necessarily related to the file system.

github.com/hajimehoshi/ebiten是模块路径的示例.

如果您正在使用Go模块,这也是Go自动从中下载它的路径.

If you're using Go modules, this is also the path Go automatically downloads it from.

如果使用$ GOPATH,则进入模块源的路径为go/src/github.com/hajimehoshi/ebiten.

If you're using $GOPATH, the path into the source of the module would be go/src/github.com/hajimehoshi/ebiten.

使用go mod init <module path>用新的模块路径初始化模块.通常,这将是您的GitHub存储库,不带https://.这将使您和您的其他代码都可以使用该模块路径进行访问.然后应该可以通过import "github.com/username/repo/myapi"访问myapi.

Initialize your module with a new module path using go mod init <module path>. Generally this will be your GitHub repository, without the https://. This will allow for both your and others code to be able accessible using that module path. myapi should then be accessible via import "github.com/username/repo/myapi".

如果您仍然希望使用旧的$ GOPATH方法,只需将代码放在go/src/<module path>中.等同于访问myapi的方法.

If you still wish to use the old $GOPATH method, simply place your code inside go/src/<module path>. The method of accessing myapi is equivalent.

阅读使用Go模块

Read Using Go Modules and How to write Go code for more information.

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

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