如何编译不符合”go get”模式的Go代码?

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

How to compile go code that doesn't conform to the `go get` pattern

问题

我在我的系统上安装了Go,并且可以安装符合go get ...模式的包。正如你在这个链接中所看到的,MIT正在使用Go来进行其中一门课程。然而,安装提供的代码并不像运行go get ...那样简单,也不能将所有的包都安装在正确的位置。相反,它要求你克隆存储库,然后“编译我们提供的初始软件,并使用下载的输入文件运行它”。正如你所看到的,它指示用户导出GOPATH(我认为是假设学生们是第一次使用Go)。

git clone git://g.csail.mit.edu/6.824-golabs-2014 6.824

$ add 6.824
$ export GOPATH=$HOME/6.824
$ cd ~/6.824/src/main
$ go run wc.go master kjv12.txt sequential

当我克隆存储库并从/src/main运行go run wc.go master kjv12.txt sequential时,它找不到包。源代码(例如,应该运行的wc.go文件)似乎假设包在同一个目录中。这是在/src/main中的wc.go文件,它需要/src/mapreduce

import "os"
import "fmt"
import "mapreduce"
import "container/list"

编译这种分布式代码的最佳/最简单/最方便的方法是什么?我能想到的一种方法是进入每个包,运行go install,然后更改每个需要这些包的文件中的导入路径,但这非常耗时,我认为这不是推荐的方法,而且我也不想更改GOPATH。

英文:

I have Go installed on my system and can install packages that conform to the go get ... pattern. As you can see in this link, MIT is using Go for one of its courses. However, installing the provided code isn't as easy as running go get ... and having all the packages installed in the proper place. Rather, it asks you to clone the repository and then "Compile the initial software we provide you and run it with the downloaded input file". As you can also see, it instructs the user to export a GOPATH (I think assuming that the students are using Go for the first time)

 git clone git://g.csail.mit.edu/6.824-golabs-2014 6.824

    $ add 6.824
    $ export GOPATH=$HOME/6.824
    $ cd ~/6.824/src/main
    $ go run wc.go master kjv12.txt sequential

When I clone the repo and run go run wc.go master kjv12.txt sequential from /src/main it can't find the packages. The source code (for example, the wc.go file that was supposed to be run), seems to assume the packages are in the same directory. This is the wc.go file that's in /src/main and which needs /src/mapreduce

import "os"
import "fmt"
import "mapreduce"
import "container/list"

What's the best/easiest/most convenient way to compile code that's distributed like this? One way I can think of is to cd into each package, run go install and then change the import path in each file that requires these packages, which is very time consuming and I'm assuming is not the recommended way, and I also don't want to change the GOPATH

答案1

得分: 4

例如,按照MIT 6.824指南的说明,运行以下命令:

$ cd $HOME
$ git clone git://g.csail.mit.edu/6.824-golabs-2014 6.824
正在克隆到 '6.824'...
远程: 正在计数对象: 108, 完成.
远程: 压缩对象中: 100% (107/107), 完成.
远程: 总共 108 (差异 40), 重用 0 (差异 0)
正在接收对象: 100% (108/108), 1.61 MiB | 561.00 KiB/s, 完成.
正在解析差异: 100% (40/40), 完成.
正在检查连接... 完成.
$ cd 6.824
$ ls
Makefile  src
$ export GOPATH=$HOME/6.824
$ cd ~/6.824/src/main
$ go version
go version devel +01dfd37363e9 Fri Aug 22 22:22:16 2014 +0400 linux/amd64
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/peter/6.824"
GORACE=""
GOROOT="/home/peter/go"
GOTOOLDIR="/home/peter/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
$ go run wc.go master kjv12.txt sequential
# command-line-arguments
./wc.go:12: missing return at end of function
./wc.go:16: missing return at end of function
$ 

运行这些命令时,你得到了什么输出?


对于wc.go文件,

import "os"
import "fmt"
import "container/list"

这些引用在$GOROOT中找到,

import "mapreduce"

这个引用在$GOPATH中找到。


使用

$ export GOPATH=$HOME/6.824
英文:

For example, following the MIT 6.824 instructions,

$ cd $HOME
$ git clone git://g.csail.mit.edu/6.824-golabs-2014 6.824
Cloning into '6.824'...
remote: Counting objects: 108, done.
remote: Compressing objects: 100% (107/107), done.
remote: Total 108 (delta 40), reused 0 (delta 0)
Receiving objects: 100% (108/108), 1.61 MiB | 561.00 KiB/s, done.
Resolving deltas: 100% (40/40), done.
Checking connectivity... done.
$ cd 6.824
$ ls
Makefile  src
$ export GOPATH=$HOME/6.824
$ cd ~/6.824/src/main
$ go version
go version devel +01dfd37363e9 Fri Aug 22 22:22:16 2014 +0400 linux/amd64
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/peter/6.824"
GORACE=""
GOROOT="/home/peter/go"
GOTOOLDIR="/home/peter/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
$ go run wc.go master kjv12.txt sequential
# command-line-arguments
./wc.go:12: missing return at end of function
./wc.go:16: missing return at end of function
$ 

What output do you get when you run these commands?


For wc.go,

import "os"
import "fmt"
import "container/list"

are found in $GOROOT and

import "mapreduce"

is found in $GOPATH.


Use

$ export GOPATH=$HOME/6.824

huangapple
  • 本文由 发表于 2014年8月24日 08:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/25467458.html
匿名

发表评论

匿名网友

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

确定