Create Module
A module is a set of packages.
A package is just a group of code.
Create a module
go mod init github.com/path/to/modThe modules name must be the name of the place where it can be downloaded, (if you intent on other people downloading it)
This creates a go.mod file, that tracks the exact versions of the dependencies of this module
Create an Exported Method
package greetings
import "fmt"
func Hello(name string) string {
message := fmt.Sprintf("Hello %v", name)
return message
}An exported method is a method that has a capitalised name such as Hello these methods are callable by other packages.
Create the module that will use your custom package
package main
import (
"fmt"
"github.com/path/to/mod"
)
func main() {
message := greetings.Hello("Gladys")
fmt.Println(message)
}
Note that normally you would normally have uploaded the file to somewhere Go can fetch it.
If the module is local then you will need to modify the path.
go mod edit -replace example.com/modName=../path/to/dir/containing/mod/file