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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误