如何导入golang自己的包?

时间:2016-05-19 03:58:19

标签: go goclipse

我在导入包时遇到问题。我想导入"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

问候 haapoo

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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.

答案 2 :(得分:0)

  1. 永远不要设置GOROOT。 (除非你知道你在做什么)。设置需要。

  2. 因为你不能导入golang.org/x/tools/cmd/oracle,因为这是一个主要包。至少你不能这么简单,不管怎样你都不应该这样做(除非你想测试导入的主包)。

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

  4. 在命令行上构建,以确定您正在做什么以及如何做(IDE会在这里模糊不清)。

答案 3 :(得分:0)

oracle是一个源分析命令行工具,您无法导入它。使用以下命令获取它:

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

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

访问https://bl.ocks.org/emmasaunders以获取完整的oracle文档。