This repository contains an example of how to set up a go program with modules to use an external source library on the same system.
This repository contains two directories:
examplelib: The library with code we want to importmain: The directory containing the program we're writing. This is that program that will import the library.
Here are the most important steps:
- In
main/go.mod: Add areplaceline to tell go where the module lives and what to call it (in this case, "examplelib")
replace examplelib => PATH_TO_CODE
See main/go.mod
-
In
main/main.go, import the library using the name given (here, "examplelib"), and use it as normal -
Before building the code (with
go buildormake), tell go to updatego.modto go find and track the module, as follows:
go get examplelib
- Build the code normally--ie, run
makeorgo buildfrom themaindirectory. This should produce an executable calledmainthat uses the code inexamplelib.