Leahcim
Leahcim

Reputation: 41949

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

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

Upvotes: 1

Views: 346

Answers (1)

peterSO
peterSO

Reputation: 166626

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

Upvotes: 4

Related Questions