douqian1835 2014-08-24 00:08
浏览 56
已采纳

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

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条回答 默认 最新

  • douti8321 2014-08-24 04:27
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题