使用gore如何加载本地的Go文件?

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

How to load local go file by using gore

问题

我安装了gore来使用REPL。

我想要检查这个文件中ParseFullTimeSpec函数的行为。

https://github.com/dshearer/jobber/blob/master/jobfile/time_spec.go#L79

我尝试使用:import github.com/dshearer/jobber/jobfile/time_spec.go,但是返回了一个错误error: import: can't find import:

我还尝试了git clone之后的相对路径,但是无论如何都无法导入它。

gore中加载go文件的正确方法是什么?

将包安装到工作区

为了将包安装到我的工作区,我按照以下步骤进行:

mkdir $GOPATH/src/github.com/dshearer
cd $GOPATH/src/github.com/dshearer
git clone git@github.com:dshearer/jobber.git
go install github.com/dshearer/jobber/jobfile

它返回了一个错误:

# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd

我无法安装common包,出现了一个错误undefined: sudo_cmd

go install github.com/dshearer/jobber/common
# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd
英文:

I installed gore to use REPL.

And I want to check behavior of a ParseFullTimeSpec function in this file.

https://github.com/dshearer/jobber/blob/master/jobfile/time_spec.go#L79

I tried by :import github.com/dshearer/jobber/jobfile/time_spec.go, but it returned a error error: import: can't find import:

I also tried relative path after git clone, but I couldn't import it anyway.

What is a right way to load the go file from gore?

Installing the package to workplace

To install the package to my workplace I did like this:

mkdir $GOPATH/src/github.com/dshearer
cd $GOPATH/src/github.com/dshearer
git clone git@github.com:dshearer/jobber.git
go install github.com/dshearer/jobber/jobfile

It returns a error:

# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd

And I couldn't install common package with a error undefined: sudo_cmd.

go install github.com/dshearer/jobber/common
# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd

答案1

得分: 1

你需要导入该包本身,该文件是该包的一部分。在Go语言中,无法单独导入文件。

import github.com/dshearer/jobber/jobfile
英文:

You need to import the package itself, which that file is a part of. You can't import files separately in go.

:import github.com/dshearer/jobber/jobfile

答案2

得分: 1

所以有两个问题:

1)您可以导入包,但不能导入特定的文件(包可能包含多个文件)。

2)关于sudo_cmd的源代码和错误 - sudo_cmd仅适用于Linux和FreeBSD平台。请参见:

nkts@zulu ~ $ ls -l go/src/github.com/dshearer/jobber/common/sudo_cmd_*
-rw-rw-r--. 1 nkts nkts 214 Nov 22 22:28 go/src/github.com/dshearer/jobber/common/sudo_cmd_freebsd.go
-rw-rw-r--. 1 nkts nkts 229 Nov 22 22:28 go/src/github.com/dshearer/jobber/common/sudo_cmd_linux.go

因此,它在Windows或MacOSX上不起作用。

但在Linux上明显可以正常工作(带有自动完成):

nkts@zulu ~ $ go get -u github.com/motemen/gore
nkts@zulu ~ $ go get -v github.com/dshearer/jobber/jobfile
github.com/dshearer/jobber (download)
github.com/dshearer/jobber/Godeps/_workspace/src/gopkg.in/yaml.v2
github.com/dshearer/jobber/common
github.com/dshearer/jobber/jobfile
nkts@zulu ~ $ gore
gore version 0.2.6 :help for help
gore> :import github.com/dshearer/jobber/jobfile
gore> jobfile.ErrorHandlerStopNam^C

英文:

So there two problems:

  1. You can import packages, but not specific files (package might contain multiple files)

  2. The source code and error about sudo_cmd - sudo_cmd is available only on Linux and FreeBSD platforms. See:

    nkts@zulu ~ $ ls -l go/src/github.com/dshearer/jobber/common/sudo_cmd_*
    -rw-rw-r--. 1 nkts nkts 214 Nov 22 22:28 go/src/github.com/dshearer/jobber/common/sudo_cmd_freebsd.go
    -rw-rw-r--. 1 nkts nkts 229 Nov 22 22:28 go/src/github.com/dshearer/jobber/common/sudo_cmd_linux.go

So it should not work on Windows or MacOSX.

But clearly works on Linux (with auto complete):

nkts@zulu ~ $ go get -u github.com/motemen/gore
nkts@zulu ~ $ go get -v github.com/dshearer/jobber/jobfile
github.com/dshearer/jobber (download)
github.com/dshearer/jobber/Godeps/_workspace/src/gopkg.in/yaml.v2
github.com/dshearer/jobber/common
github.com/dshearer/jobber/jobfile
nkts@zulu ~ $ gore
gore version 0.2.6  :help for help
gore> :import github.com/dshearer/jobber/jobfile
gore> jobfile.ErrorHandlerStopNam^C

答案3

得分: 0

你最后的输出似乎显示你做得很对,但你使用的源代码状态不正确:

go install github.com/dshearer/jobber/common
# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd

从jobber的源代码来看,它似乎有一个Makefile,该文件应该为你的架构添加缺失的函数。

这个项目实际上有特定的安装说明,你可以在这里找到它们

cd /path/to/your/workspace
go get github.com/dshearer/jobber
cd src/github.com/dshearer/jobber
git checkout v1.1
make GO_WKSPC=/path/to/your/workspace

注意,在你的$GOPATH中安装依赖的规范方式是使用go get

go get host.com/namespace/package

然而,在这里这样做是不够的,因为包的作者决定使用了一个临时的构建机制。

英文:

Your last output seems to show you're doing it right, but the source code you're using is not in proper state:

go install github.com/dshearer/jobber/common
# github.com/dshearer/jobber/common
src/github.com/dshearer/jobber/common/sudo.go:15: undefined: sudo_cmd

Looking at the sources for jobber, it appears it has a Makefile, which is supposed to add the missing function for your architecture.

This project actually has specific install instructions, you can find them here.

cd /path/to/your/workspace
go get github.com/dshearer/jobber
cd src/github.com/dshearer/jobber
git checkout v1.1
make GO_WKSPC=/path/to/your/workspace

PS: note that the canonical way to install dependency in your $GOPATH is to use go get:

go get host.com/namespace/package

This is not enough here, though, as package author decided to have an adhoc build mechanism.

huangapple
  • 本文由 发表于 2016年11月11日 01:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/40533859.html
匿名

发表评论

匿名网友

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

确定