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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题