我在尝试导入Go包时遇到了错误。谁可能是这个问题的适当解决方案?

huangapple go评论79阅读模式
英文:

i am getting error while trying to import go package. who could be appropriate solution for this problem?

问题

无法导入myproject/game/helper2(在任何位置都找不到包“myproject/game/helper2”)
/usr/local/go/src/myproject/game/helper2(来自$GOROOT)
/home/prasnjit/dev/go/src/myproject/game/helper2(来自$GOPATH)编译器

以下是代码的屏幕截图
屏幕截图如下所示。给我一些有用的解决方案?点击此处查看图片描述

我正在尝试在main.go中导入一个包,但在导入时出现错误。我无法确定问题可能是什么。

英文:

could not import myproject/game/helper2 (cannot find package “myproject/game/helper2” in any of
/usr/local/go/src/myproject/game/helper2 (from $GOROOT)
/home/prasnjit/dev/go/src/myproject/game/helper2 (from $GOPATH))compiler

the screen shot of code is attached below
screenshort is given below. Give me some useful solution of it? enter image description here

i am trying to import one package in main.go and getting error while importing. i am not able to configure what could be the problem.

答案1

得分: 1

我会假设你的模块名为"myproject"(在go.mod中声明)。看起来你想要导入"game"包。

package main

import (
    "fmt"
    "myproject/game"
)

func main() {
    fmt.Println(game.Get())
}

在Go语言中,你导入的是_包_,而不是文件。包通常组织在目录中。Go源文件在顶部声明它们所属的包。在这里了解更多关于Go模块的信息:链接

英文:

I'll assume your module is called "myproject" (this is declared in go.mod). It looks like you want to import the "game" package instead.

package main

import (
 "fmt"
 "myproject/game"
)

func main() {
  fmt.Println(game.Get())
}

In Go, you import packages, not files. Packages are typically organized in directories. Go source files declare the package that they belong to on top. Learn more about Go modules here.

答案2

得分: 0

看起来你导入代码的方式不正确。

尝试像这样导入:

import "myproject/game"

如果你正在导入一个包,不需要提及文件名。
查看这个链接

英文:

It seems like you are doing the import wrong.

try like this

import "myproject/game"

If you are importing a package,no need to mention the filename.
Checkout this

huangapple
  • 本文由 发表于 2023年3月6日 18:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75649518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定