It seems that the way modules are used since 1.11 has changed , and I am trying to understand how to reference a module / package from another directory.
Lets say I have a folder structure \root\module1 \root\module2
I have a go.mod in each directory and I can access / use those modules from the \root directory
How can I access module2 from module1. The modules are not published anywhere ( nor do I want them to be ) - I just want to access them. Module 2 contains types / structs that I need to use in mondule1
Kind Regards Martin
Using packages, you organize your code into reusable units. A module, on the other hand, is a collection of Go packages, with dependencies and versioning built-in. Modules were first introduced in Go 1.11, but it wasn't until Go 1.16 that the go command builds packages in module-aware mode by default.
Go modules have to be placed in GOPATH for be used.
When i start a new go project, i usually create a folder into the gopath
cd $GOPATH
ls
Here you find 3 folder
bin pkg src
ls src
>code.cloudfoundry.org github.com github.ibm.com golang.org gopkg.in go.uber.org honnef.co winterdrache.de
Into src, there are the code that you retrieve using 'go get' command.
Everything that is here can be imported(/exported) into your software.
Assume this test project:
github.ibm.com/
└── Alessio-Savi
└── GoLog-Viewer
├── conf
│ ├── dev.json
│ └── test.json
├── database
│ ├── cloudant
│ │ └── cloudant.go
│ └── db2
│ └── db2.go
├── datastructure
│ └── datastructures.go
├── GinProva.go
├── README.md
├── request
│ └── request.go
└── resources
└── template01.html
NOTE: Data structure are saved in a go file in a properly directory for avoid circle-import
You can import the datastructures.go (or another file that you need) using the following import statement
package mypackage
import(
"github.ibm.com/Alessio-Savi/GoLog-Viewer/datastructure"
)
In other file (in the same project as in other) you can simply use the package and let the IDE help you (due to the fact the the module/project is in GOPATH)
In order to create a new module, you can use the new go module init
gotool command.
A common way for create a new module, in case of public source code, is the follwing:
go mod init github.com/username/modulename
This will generate two file:
The go.mod
file will contain every library/external golang code necessary to run your module.
The go.sum
file will contain the hash of the library.
I'll use for example my little general purpose library, called GoGPUtils
.
mkdir GoGPUtils
cd $_
go mod init github.com/alessiosavi/GoGPUtils
Now, you can insert the library that you need in your code in the go.mod
library. Assume that you need the ahocorasick
implementation for work with string search, the go.mod
file will contains the following content:
module github.com/alessiosavi/GoGPUtils
go 1.13
require (
github.com/alessiosavi/ahocorasick v0.0.3
golang.org/x/tools v0.0.0-20191031220737-6d8f1af9ccc0 // indirect
)
In the require
section, there are the list of package needed. Now you can import the ahocorasick
library in your code as following:
import (
ahocorasick "github.com/alessiosavi/ahocorasick"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With