简体   繁体   中英

How to develop and test a local go module

I have this script:

#!/usr/bin/env bash

set -eo pipefail

cd "$(dirname "$BASH_SOURCE")";
pth_to_make="$GOPATH/src/ores/json-logging"
mkdir -p "$pth_to_make";
rm -rf "$pth_to_make";
ln -sf "$PWD" "$pth_to_make";

however this module is not declared as a go module in go.mod...I am symlinking a different dependency into the repo to test it.

The real dependency in production is at:

"$GOPATH/src/github.com/oresoftware/json-logging"

the test path is:

"$GOPATH/src/ores/json-logging"

Now, I would just delete the real dependency path in local development, the problem is that it then just deletes the .git folder in the repo and then I end up having to download that again.

Is there some way for me to symlink over the real repo/dep folder, but not lose the git stuff? Maybe I could do:

 mv   "$GOPATH/src/github.com/oresoftware/json-logging"  "/tmp/gotemp/json-logging"
 # call the script above
 # then when I am done:
 rm -rf "$GOPATH/src/github.com/oresoftware/json-logging"
 mv "/tmp/gotemp/json-logging" "$GOPATH/src/github.com/oresoftware/json-logging"  # put it back

would that be the best way forward?

So to expand on my comment it sounds like what you are after is the replace directive which you can read more about here . But i've outlined a brief example below;

If we imagine the directory structure:

├── replace-directive
│   ├── go.mod
│   └── replace.go
├── sandbox
│   ├── go.mod
│   └── main.go

The replace-directive is a go module with the go.mod file containing:

module example.com/replacedirective

go 1.14

Sandbox is where we want to use this local dependency version rather than grabbing the version from example.com . So in our sandbox go.mod file we have the replace directive as follows;

module example/sandbox

require (
  example.com/replacedirective v0.0.0  // This would be your reference to your production dependency
)

replace example.com/replacedirective => ../replace-directive // This is your local dependency

go 1.14

So now when you reference the module in your code it will use your local dependency instead. No symlinks required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM