1

我在导入包时遇到问题。我想导入"golang.org/x/tools/cmd/oracle""golang.org/x/tools/oracle"包。

导入这些包的正确方法是什么?


这是我尝试导入包的方式:

package main

import (
    "fmt"
    "strings"
    "os"
    "os/exec"
    "golang.org/x/tools/oracle"
    "golang.org/x/tools/cmd/oracle"
)

最后两个导入导致以下问题:

====================  Starting Go build  ====================
Cleared problem markers for ProjektGO.
************  Building Go project: ProjektGO  ************
  with GOPATH: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO
>> Running: /usr/lib/go/bin/go install -v -gcflags "-N -l" ./...
file/main.go:13:2: cannot find package "golang.org/x/tools/cmd/oracle" in any of:
    /usr/lib/go/src/golang.org/x/tools/cmd/oracle (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/cmd/oracle (from $GOPATH)
file/main.go:12:2: cannot find package "golang.org/x/tools/oracle" in any of:
    /usr/lib/go/src/golang.org/x/tools/oracle (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle (from $GOPATH)
   ^^^ Terminated, exit code: 1 ^^^
************  Build terminated.  ************

然后我尝试了这样的事情:

package main

import (
    "fmt"
    "strings"
    "os"
    "os/exec"
    "../../../../../golang.org/x/tools/cmd/oracle"
    "../../../../../golang.org/x/tools/oracle"
)

结果是这样的:

====================  Starting Go build  ====================
************  Building Go project: ProjektGO  ************
  with GOPATH: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO
>> Running: /usr/lib/go/bin/go install -v -gcflags "-N -l" ./...
can't load package: /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/file/main.go:11:2: import "../../../../../golang.org/x/tools/cmd/oracle" is a program, not an importable package
../../../../golang.org/x/tools/oracle/describe.go:20:2: cannot find package "golang.org/x/tools/go/ast/astutil" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/ast/astutil (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ast/astutil (from $GOPATH)
../../../../golang.org/x/tools/cmd/oracle/main.go:26:2: cannot find package "golang.org/x/tools/go/buildutil" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/buildutil (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/buildutil (from $GOPATH)
../../../../golang.org/x/tools/oracle/callers.go:11:2: cannot find package "golang.org/x/tools/go/callgraph" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/callgraph (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/callgraph (from $GOPATH)
../../../../golang.org/x/tools/cmd/oracle/main.go:27:2: cannot find package "golang.org/x/tools/go/loader" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/loader (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/loader (from $GOPATH)
../../../../golang.org/x/tools/oracle/callees.go:17:2: cannot find package "golang.org/x/tools/go/pointer" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/pointer (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/pointer (from $GOPATH)
../../../../golang.org/x/tools/oracle/callees.go:18:2: cannot find package "golang.org/x/tools/go/ssa" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/ssa (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ssa (from $GOPATH)
../../../../golang.org/x/tools/oracle/callees.go:19:2: cannot find package "golang.org/x/tools/go/ssa/ssautil" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/ssa/ssautil (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/ssa/ssautil (from $GOPATH)
../../../../golang.org/x/tools/oracle/describe.go:22:2: cannot find package "golang.org/x/tools/go/types/typeutil" in any of:
    /usr/lib/go/src/golang.org/x/tools/go/types/typeutil (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/go/types/typeutil (from $GOPATH)
../../../../golang.org/x/tools/cmd/oracle/main.go:28:2: cannot find package "golang.org/x/tools/oracle" in any of:
    /usr/lib/go/src/golang.org/x/tools/oracle (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle (from $GOPATH)
../../../../golang.org/x/tools/oracle/callees.go:20:2: cannot find package "golang.org/x/tools/oracle/serial" in any of:
    /usr/lib/go/src/golang.org/x/tools/oracle/serial (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/oracle/serial (from $GOPATH)
../../../../golang.org/x/tools/oracle/implements.go:21:2: cannot find package "golang.org/x/tools/refactor/importgraph" in any of:
    /usr/lib/go/src/golang.org/x/tools/refactor/importgraph (from $GOROOT)
    /home/haapoo/go/src/github.com/ProjektarbeitGo/ProjektGO/src/golang.org/x/tools/refactor/importgraph (from $GOPATH)
   ^^^ Terminated, exit code: 1 ^^^
************  Build terminated.  ************

我的 GOROOT 是/usr/lib/go,我的 GOPATH 是/home/haapoo/go. 我知道关于 gopath 和 goroot 的问题经常出现,但也许有人有想法。

PS:我用goclipse

问候哈普

4

4 回答 4

2

你跑过go get golang.org/x/tools/oraclego get golang.org/x/tools/cmd/oracle?这应该在$GOPATH正确设置后运行。

于 2016-05-19T04:17:13.887 回答
1
  1. 永远不要设置 GOROOT。(除非你知道你在做什么)。不需要设置它。

  2. 因为你不能导入 golang.org/x/tools/cmd/oracle 因为这是一个主包。至少你不能以简单的方式做到这一点,而且你也不应该这样做(除非你想测试导入的主包)。

  3. 导入并不神奇,您必须在使用前下载包。go get像其他人提到的那样使用。

  4. 在命令行上构建以确保您在做什么以及您是如何做的(IDE 往往会在此处模糊事物)。

于 2016-05-19T05:57:48.837 回答
0

oracle是源码分析命令行工具,不能导入。使用以下命令获取它:

go get golang.org/x/tools/cmd/oracle

oracle这将在您的 $GOPATH/bin 目录中创建一个名为的可执行文件。

访问http://golang.org/s/oracle-user-manual以获取完整的oracle文档。

于 2016-05-19T10:12:32.217 回答
0

如果需要在 Golang 中导入不同的同名包,可以这样(直接命名):

import (
    "text/template"
    ht "html/template"
)

但如果是“golang.org/x/tools/cmd/oracle”
,它不是一个包,它只有 main.go,你需要构建/安装它。它是一个命令行工具:

Run 'oracle -help' for more information.
Go source code oracle.
Usage: oracle [<flag> ...] <mode> <args> ...

The -format flag controls the output format:
        plain   an editor-friendly format in which every line of output
                is of the form "pos: text", where pos is "-" if unknown.
        json    structured data in JSON syntax.
        xml     structured data in XML syntax.
于 2016-05-19T04:15:44.503 回答